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

3 comments
That's because you need to move the while loop then into the event connection, change expTime to be equal to some variable tracking time, etc. A little too long for this answer.
Okay I submitted and decided to give an expectation of what you expected to run.
I fixed it, if you'd like to see what I did you can click this link: https://hastebin.com/yorukutivi.lua