Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

While Loop Crashes RBX Studio?

Asked by 6 years ago

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

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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)
0
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. User#18718 0 — 6y
0
Okay I submitted and decided to give an expectation of what you expected to run. User#18718 0 — 6y
0
I fixed it, if you'd like to see what I did you can click this link: https://hastebin.com/yorukutivi.lua Mikel5656 2 — 6y
Ad

Answer this question