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

Why doesn't my condition for my if statement and repeat loop work?

Asked by 8 years ago
Edited 8 years ago

I'm trying to make it so the player would gain 1 money every second, but its not looping correctly. This is what I'm using:

01local leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats")
02local money = leaderstats.Money
03 
04money.Value.Changed:connect(function()
05    if money.Value < 1000 then
06        repeat
07            money.Value = money.Value + 1
08            wait(1)
09        until money.Value == 1000
10    end
11end)
12money.Value = money.Value - 1

Any help is greatly appreciated!

1 answer

Log in to vote
0
Answered by
k3du53 162
8 years ago

This is simply a mistake in the coding. (peep the comment lines)

01local leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats")
02local money = leaderstats.Money
03 
04money.Value.Changed:connect(function() --every time the money gets changed, this function runs
05    if money.Value < 1000 then
06        repeat
07            money.Value = money.Value + 1 --within the function, the money is being changed, causing the function to repeat itself more than intended
08            wait(1)
09        until money.Value == 1000
10    end
11end)
12money.Value = money.Value - 1

A solution could be to remove the repeat statement, or repeat the if statement.

01local leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats")
02local money = leaderstats.Money
03 
04money.Value.Changed:connect(function()
05    if money.Value < 1000 then
06        money.Value = money.Value + 1
07        wait(1)
08    end
09end)
10money.Value = money.Value - 1
0
Sorry, but it still doesn't work. I don't know how to fix this... marioblast1244 113 — 8y
Ad

Answer this question