Still have questions? Join our Discord server and get real time help.
1

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

Edited 3 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:

local leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats")

money.Value.Changed:connect(function()
if money.Value < 1000 then
repeat
money.Value = money.Value + 1
wait(1)
until money.Value == 1000
end
end)
money.Value = money.Value - 1


Any help is greatly appreciated!

0
k3du53 110
3 years ago

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

local leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats")

money.Value.Changed:connect(function() --every time the money gets changed, this function runs
if money.Value < 1000 then
repeat
money.Value = money.Value + 1 --within the function, the money is being changed, causing the function to repeat itself more than intended
wait(1)
until money.Value == 1000
end
end)
money.Value = money.Value - 1


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

local leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats")