I've made a script to exponentially grow your cash in a game I'm working on. It's supposed to update the value of cash on the scoreboard. For some reason, when I try to loop it, it freezes. Even when wait() is used with a high number. What am I doing wrong?
local i = 1 while i == 1 do local percent = 4 -- Percentage to multiply by. local base = 800 -- Base value. local expTime = 1 -- Time in units to use as the exponent. local differ = base + (base * (percent / 100)) -- To change to exponential growth, change the - to +. local rate = differ / base local answer = base * rate ^ expTime local newAnswer = answer>=0 and math.floor(answer+0.5) or math.ceil(answer-0.5) game.Players.PlayerAdded:connect(function(player) local leaderstats = Instance.new("IntValue") leaderstats.Name = "leaderstats" leaderstats.Value = 0 local cash = Instance.new("StringValue") cash.Name = "cash" cash.Value = newAnswer leaderstats.Parent = player cash.Parent = leaderstats wait(10) end) end
Perhaps the reason this is happening is because you create new event connections every execution of your while loop and the wait occurs within that event connection.
Take a look at your loop. The way your code is structured you have.
while
- do calculation
- event connection
- wait
The wait function will now only run when the event is run (and that's actually done in a seperate thread so it won't wait in the while function).
The way to correct this is to move your event connection outside the while loop and leave a wait() or wait(10) within the while loop.
local i = 1 while i == 1 do local percent = 4 -- Percentage to multiply by. local base = 800 -- Base value. local expTime = 1 -- Time in units to use as the exponent. local differ = base + (base * (percent / 100)) -- To change to exponential growth, change the - to +. local rate = differ / base local answer = base * rate ^ expTime local newAnswer = answer>=0 and math.floor(answer+0.5) or math.ceil(answer-0.5) wait(10) -- left this here end -- moved this block outside game.Players.PlayerAdded:connect(function(player) local leaderstats = Instance.new("IntValue") leaderstats.Name = "leaderstats" leaderstats.Value = 0 local cash = Instance.new("StringValue") cash.Name = "cash" cash.Value = newAnswer leaderstats.Parent = player cash.Parent = leaderstats end)
EDIT You suggested that the script was not working the way you expected. Perhaps this is what you expected.
game.Players.PlayerAdded:connect(function(player) local i = 1 local leaderstats = Instance.new("IntValue") leaderstats.Name = "leaderstats" leaderstats.Value = 800 local cash = Instance.new("StringValue") cash.Name = "cash" cash.Value = newAnswer leaderstats.Parent = player cash.Parent = leaderstats local expTime = 0 -- Time in units to use as the exponent. while i == 1 do expTime = expTime + 1 local percent = 4 -- Percentage to multiply by. local base = 800 -- Base value. local differ = base + (base * (percent / 100)) -- To change to exponential growth, change the - to +. local rate = differ / base local answer = base * rate ^ expTime local newAnswer = answer>=0 and math.floor(answer+0.5) or math.ceil(answer-0.5) leaderstats.Value = newanswer wait(1) end end)