I have no idea why this is not working. I am trying to delete the numbers in the table one by one (the randomCheck picks a number from the table and I want to delete that exact number) when I press the button 'MyButton'
math.randomseed(os.time())
_X = display.contentCenterX
_Y=display.contentCenterY
local numbers = {1,2,3,4,5,6,7,8,9,10}
local randomCheck = numbers[ math.random( #numbers) ]
local text = display.newText(""..randomCheck.."",_X,_Y,"Helvetica",50)
function removeNumber()
for i = 1, 10 do
if(randomCheck == i ) then
table.remove(numbers,i)
text.text = (""..i.."")
end
end
end
myButton = display.newRect(_X,_Y+100,100,80 )
myButton:addEventListener("tap", removeNumber)
In your loop, instead of
if(randomCheck == i)
use
if(randomCheck == numbers[i])
But all of that work is really unnecessary.
Instead of
local randomCheck = numbers[math.random( #numbers)]
use
local randomIndex = math.random(#numbers)
local randomCheck = numbers[randomIndex]
Then you can just
table.remove(numbers, randomIndex)
Related
I have no idea how this is an error, can anyone help me out? This is supposed to be a global leaderboard, but nope, this error is ruining it. The line is new.ClickVal.Text = val. it used to be new.Value.Text = val, and i thought maybe it was because the game thought 'Value' was the actual VALUE of the text, which it was the name of a textlabel, so i changed it to 'ClickVal" and it still doesnt work. Thank you for helping out!!!
local sg = script.Parent
local sample = script:WaitForChild("Sample")
local sf = sg:WaitForChild("ScrollingFrame")
local ui = sf:WaitForChild("UI")
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetOrderedDataStore("Leaderboard")
wait(10)
while true do
for i,plr in pairs(game.Players:GetChildren()) do
if plr.UserId>0 then
local ps = game:GetService("PointsService")
local w = plr.leaderstats.Clicks.Value
if w then
pcall(function()
dataStore:UpdateAsync(plr.UserId,function(oldVal)
return tonumber(w)
end)
end)
end
end
end
local smallestFirst = false
local numberToShow = 100
local minValue = 1
local maxValue = 10e100
local pages = dataStore:GetSortedAsync(smallestFirst, numberToShow, minValue, maxValue)
local top = pages:GetCurrentPage()
local data = {}
for a,b in ipairs(top) do
local userId = b.key
local points = b.Value
local username = "[Failed To Load]"
local s,e = pcall(function()
username = game.Players:GetNameFromUserIdAsync(userId)
end)
if not s then
warn("Error getting name for "..userId..". Error: "..e)
end
local image = game.Players:GetUserThumbnailAsync(userId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size150x150)
table.insert(data,{username,points,image})
end
ui.Parent = script
sf:ClearAllChildren()
ui.Parent = sf
for number,d in pairs(data) do
local name = d[1]
local val = d[2]
local image = d[3]
local color = Color3.new(1,1,1)
if number == 1 then
color = Color3.new(1,1,0)
elseif number == 2 then
color = Color3.new(0.7,0.7,0.7)
elseif number == 3 then
color = Color3.fromRGB(166, 112, 0)
end
local new = sample:Clone()
new.Name = name
new.LayoutOrder = number
new.Image.Image = image
new.Image.Place.Text = number
new.Image.Place.TextColor3 = color
new.PName.Text = name
new.ClickVal.Text = val
new.ClickVal.TextColor3 = color
new.PName.TextColor3 = color
new.Parent = sf
end
wait()
sf.CanvasSize = UDim2.new(0,0,0,ui.AbsoluteContentSize.Y)
wait(10)
end
if you have the fix then post it please thanks!
I'm sorry if this is a really simple or easy question, but I am trying to set the player leaderstat "Money" to 0, but it keeps coming back as the old value. The only thing that I can think of that may be causing this problem is that the moneyGain script is on repeat.
Here is my moneyGain script:
local playerMoney
while wait(0.24) do
local buttons = script.Parent.Parent.info.savedItems.Value.buttons:GetChildren()
for button = 1, #buttons, 1 do
if buttons[button].Parent.Parent.tycoon.Value ~= nil then
if buttons[button].Parent.Parent.tycoon.Value.info.owner ~= nil then
if buttons[button].jobDone.Value == true then
script.Parent.moneyEnforcer.enforce:Invoke(buttons[button].gainVal.Value)
end
end
end
end
--OWNERDOOR:
if script.Parent.Parent.info.owner.Value ~= nil then
script.Parent.moneyEnforcer.enforce:Invoke(1)
end
end
Here is my moneyEnforcer script:
local rs = game:GetService("ReplicatedStorage")
local dictionary = require(rs.dictionary)
local vipPlayers = dictionary.vipPlayers
script.enforce.OnInvoke = function(amount)
print(amount)
if script.Parent.Parent.info.player.Value ~= nil then
if game:GetService("GamePassService"):PlayerHasPass(script.Parent.Parent.info.player.Value, 25494219) then
amount = amount * 2
else
for i = 1, #vipPlayers, 1 do
if script.Parent.Parent.info.player.Value.Name == vipPlayers[i] then
amount = amount * 2
end
end
end
local playerMoney = script.Parent.Parent.info.player.Value.leaderstats.Money
script.Parent.Parent.info.player.Value.leaderstats.Money.Value = script.Parent.Parent.info.player.Value.leaderstats.Money.Value + amount
end
end
And here is the tycoon reseter:
local player = script.Parent.Parent.Parent.Parent.Parent.Parent
script.Parent.Parent.Parent.Visible = false
script.Parent.MouseButton1Click:Connect(function()
player.leaderstats.Money.Value = 0
script.Parent.Parent.Parent.Visible = false
local savedItems = player.claimedTycoon.Value.info.savedItems.Value
local builds = player.claimedTycoon.Value.activeBuilds:GetChildren()
local buttons = player.claimedTycoon.Value.activeButtons:GetChildren()
for i = 1, #builds, 1 do
builds[i].Parent = savedItems.builds
end
for j = 1, #buttons, 1 do
buttons[j].Parent = savedItems.buttons
end
savedItems.builds.dirtStarterBase.Parent = player.claimedTycoon.Value
player.claimedTycoon.Value.info:WaitForChild("activeWave").Value = 1
end)
I have tried putting the "script.Parent.Parent.info.player.Value.leaderstats.Money.Value = script.Parent.Parent.info.player.Value.leaderstats.Money.Value + amount" on the moneyGain script, but that made no difference.
Any help is appriciated!
I believe your problem is that you're setting the value of money from the client. When you change the value from a local script, it will not update on the server. To fix this update the value from the server
In tycoon reseter, try resetting the value from the server using a RemoteEvent
So I want to add my coin object to my gameGroup via a loop,instead of adding each one manually line by line
What I'm trying to do now clearly would not work because you can't add a string to group of objects
Is there a way to do this?
local coin1 = getCoin(6000,500)
local coin2 = getCoin(850,550)
local coin3 = getCoin(980,550)
local coin4 = getCoin(1900,550)
local coin5 = getCoin(2850,380)
local coin6 = getCoin(3199,486)
local coin7 = getCoin(3543,455)
local coin8 = getCoin(3780,380)
local coinOverlay1 = display.newRect( 850,550,80,80 )
physics.addBody( coinOverlay1, { bounce=0 } )
coinOverlay1.isVisible = false
gameGroup:insert(coinOverlay1)
sceneGroup:insert( coinGroup )
coinGroup:insert( coin1 ) -- Not this would take up too many lines for each coin,there will be over 40 coins
coinGroup:insert(coin2)
camera:add( coinGroup, 1 )
for i=1,8 do
local test_1 = "coin"
local test_2 = i
local test_3 = "coin"..test_2
print (test_3)
coinGroup:insert(test_3)
end
something like this?
coins = {
{6000,500},
{850,550},
{980,550},
{1900,550},
{2850,380},
{3199,486},
{3543,455},
{3780,380}
}
for i,v in ipairs(coins) do
coinGroup:insert(getCoin(v[1],v[2]))
end
I have a lot of variables that seem to lend themselves to be referenced a lot easier, but I'm not sure how to do it; the variable names are related to entries on a coordinate system, and where they are written to is also coordinate based. I have tried searching google, I've gone through the Lua documentation, but as I'm not sure what I'm looking for I think that is hindering my search. Here is how the variables look in an excerpt of my code:
-- Bank 1
local Object111 = sItem.getTargetDetails("-4,3,-4")
local Object112 = sItem.getTargetDetails("-5,3,-4")
local Object113 = sItem.getTargetDetails("-6,3,-4")
local Object114 = sItem.getTargetDetails("-7,3,-4")
local Object115 = sItem.getTargetDetails("-8,3,-4")
local Object121 = sItem.getTargetDetails("-4,4,-4")
local Object122 = sItem.getTargetDetails("-5,4,-4")
local Object123 = sItem.getTargetDetails("-6,4,-4")
local Object124 = sItem.getTargetDetails("-7,4,-4")
local Object125 = sItem.getTargetDetails("-8,4,-4")
local Object131 = sItem.getTargetDetails("-4,5,-4")
local Object132 = sItem.getTargetDetails("-5,5,-4")
local Object133 = sItem.getTargetDetails("-6,5,-4")
local Object134 = sItem.getTargetDetails("-7,5,-4")
local Object135 = sItem.getTargetDetails("-8,5,-4")
-- Bank 2
local Object211 = sItem.getTargetDetails("-4,3,1")
local Object212 = sItem.getTargetDetails("-5,3,1")
local Object213 = sItem.getTargetDetails("-6,3,1")
local Object214 = sItem.getTargetDetails("-7,3,1")
local Object215 = sItem.getTargetDetails("-8,3,1")
local Object221 = sItem.getTargetDetails("-4,4,1")
local Object222 = sItem.getTargetDetails("-5,4,1")
local Object223 = sItem.getTargetDetails("-6,4,1")
local Object224 = sItem.getTargetDetails("-7,4,1")
local Object225 = sItem.getTargetDetails("-8,4,1")
local Object231 = sItem.getTargetDetails("-4,5,1")
local Object232 = sItem.getTargetDetails("-5,5,1")
local Object233 = sItem.getTargetDetails("-6,5,1")
local Object234 = sItem.getTargetDetails("-7,5,1")
local Object235 = sItem.getTargetDetails("-8,5,1")
I would then proceed to call each of these variables in a function, where the position on screen is a function of the numbers in the name, the first two numbers relating to x coords and the last number to the y coords:
mon.setCursorPos(4,4)
mon.write(Object111.StoredPercentage)
mon.setCursorPos(10,4)
mon.write(Object112.StoredPercentage)
mon.setCursorPos(16,4)
mon.write(Object113.StoredPercentage)
...
mon.setCursorPos(8,4)
mon.write(Object121.StoredPercentage)
mon.setCursorPos(8,4)
mon.write(Object121.StoredPercentage)
...
mon.setCursorPos(36,4)
mon.write(Object211.StoredPercentage)
mon.setCursorPos(42,4)
mon.write(Object212.StoredPercentage)
mon.setCursorPos(48,4)
mon.write(Object213.StoredPercentage)
etc....
I can see that this should be able to be generated on the fly, but I don't know where to start without calling it out manually; I would prefer it if my code was cleaner. At this stage I really just need to be taught how to fish; if anyone can point me to documents that explain how to do what I'm trying to I would be most grateful.
Here's my final solution for anyone trying to achieve same:
local Banks = 2
local Rows = 3
local Columns = 5
function Objectstatus(bank,row,column)
Id = bank .. row .. column
worldx = "-" .. column+3
worldy = row+2
if bank == 1 then worldz = -4; end
if bank == 2 then worldz = 1; end
Object = {}
Object[Id] = s.getTargetDetails(worldx..","..worldy..","..worldz)
xcursor = (column*7)+3
if bank == 1 then
ycursor = (row*4)+8
end
if bank == 2 then
ycursor = (row*4)+24
end
mon.setCursorPos(xcursor,ycursor)
powertext(Object[Id].StoredPercentage) -- A function that sets Texts settings based on result
mon.write(Object[Id].StoredPercentage) -- Actually writes the result
end
for BankTemp = 1, Banks do
for ColumnTemp = 1, Columns do
for RowTemp = 1, Rows do
Objectstatus(BankTemp,RowTemp,ColumnTemp)
end
end
end
OK my first question was too vague so I'm going to start simple here. I am trying to get a random word from a table in another lua file (content.lua). I have gotten the code to run without errors but cannot get a word to display on the screen or via print in the command console. What am I missing?
game.lua
--lua for game
--Loading the local variables
--creates the storyboard variable and calls the storyboard api
local storyboard = require ("storyboard")
--calls the mydata.lua module
local myData = require( "mydata" )
--calls the sfx.lua where sounds are stored
local sfx = require( "sfx" )
--calls the operations.lua
local operations = require("operations")
local content = require("content")
local playOrder
local wordGraphic
local currQuestion = 1
local homeButton
--tells storyboard to create a new scene
local scene = storyboard.newScene()
function scene:createScene(event)
local gameScreen = self.view
--creates a transparent background image centered on the display
local gameBackground = display.newImage("images/graphics/jungle1.jpg")
gameBackground.x = display.contentWidth/2
gameBackground.y = display.contentHeight/2
gameScreen:insert(gameBackground)
homeButton = display.newImage("images/buttons/home.png")
homeButton.alpha = .8
homeButton.y = 70
gameScreen:insert(homeButton)
playOrder = operations.getRandomOrder(#content)
end
local function onHomeTouch(event)
if event.phase == "began" then
storyboard.gotoScene("start")
end
end
function scene:enterScene(event)
homeButton:addEventListener("touch", onHomeTouch)
audio.play(sfx.Bkgd)
--uses the operations.lua to get words in a random order from the content.lua
--shows a random word from the content.lua table
function showWord()
local word = content[playOrder[currQuestion]].word
print(word)
wordGraphic = self.view
wordGraphic:insert(word)
wordGraphic.x = display.contentWidth/2
wordGraphic.y = display.contentHeight/2
end
end
--next question function which clears the screen and loads a new random word
function scene:exitScene(event)
homeButton:removeEventListener("touch", onHomeTouch)
end
function scene:destroyScene(event)
end
--the actual event listeners that make the functions work
scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("destroyScene", scene)
return scene
Here is the operations.lua that gets the random order function
--operations.lua
module(..., package.seeall)
--function to get a random piece of data
function getRandomOrder(amount)
local order ={}
local i
local temp
local temp1
for n = 1,amount do
order[n] = n
end
for i=0,9 do
for temp = 1,amount do
n = math.random(1, amount)
temp1 = order[temp]
order[temp] = order[n]
order[n] = temp1
end
end
return order
end
This is where the words I am attempting to display are stored. I did not include all of them.
--content.lua
return {
{
id = "after",
word = "after"
},
{
id = "again",
word = "again"
},
{
id = "an",
word = "an"
},
{
id = "any",
word = "any"
},
{
id = "ask",
word = "ask"
},
{
id = "as",
word = "as"
},
{
id = "by",
word = "by"
}
}
You're not calling showWord in any of the code that you've shown so far. This is probably why it isn't even printing to the console. The function is just contained within scene:enterScene and exits to the outer scope, defining itself as a global variable when enterScene is called.