Script exhausts when I change a vallue

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    This script is supposed to reward the player adding onto their leaderstats cash every 2 seconds. The higher “SeedAge” has its value set the more cash is rewarded.
  2. What is the issue?
    When SeedAge has its value changed, The Studio Testing window crashes and prints out that the script has exhausted.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried insert wait() into multiple areas of the script but it didn’t change anything. I don’t truly understand why it isn’t working.
cooldown = true
SeedAge.Changed:Connect(function()
	while SeedAge.Value >0 do
		if cooldown == true then
			cooldown = false
		local playername = script.Parent.Playername.Value
		local folder = game.Players[playername]:WaitForChild("leaderstats")
		local currency = folder:WaitForChild("$")
	wait(2)
		if SeedAge.Value == 4 then
			currency.Value = currency.Value + 30	
		end
		if SeedAge.Value == 3 then 
			currency.Value = currency.Value + 25
		end
		if SeedAge.Value == 2 then 
			currency.Value = currency.Value + 20
		end
		if SeedAge.Value == 1 then 
			currency.Value = currency.Value + 15
				wait()
				cooldown = true
			end	
			end
	end
end)

any help appreciated

1 Like

With while (true) do loops, people tend to just add a wait() or however long they need it to stop for.

Your cooldown variable isn’t gonna slow it down because think about it… while true do isn’t doing the same thing multiple times at one time. It’s going through like
Start > A > B > C > Finish > Start > A > B > C > Finish > (Keeps repeating til it errors.)

What I’m getting at is… the cooldown = true / cooldown = false (Basically, it’s just going to allow the loop to continue it’s fast course because it marks it true at the end.) isn’t gonna slow it down, so you need to add a wait.

A wait() would do just fine too. Just realize this after typing all that…

if SeedAge.Value == 1 then
currency.Value = currency.Value + 15
wait()
cooldown = true
end
end
end

Your cooldown = true statement is under a if statement for a seedage of 1. So what might be your problem is, there’s a seedage that’s not = 1 which causes your script to keep running into this statement at the top if cooldown == true then and not stop at all which causes it to exhaust.

I think your fix might be to get rid of the cooldown check and just add a wait(2) under the while SeedAge.Value >0 do at the top?

I originally put the cooldown to stop multiple loops from starting, I didn’t catch on to the cooldown under the seedage 1 statement so thank you for that, I had another problem where the loop wouldn’t start at all after its first time looping and I messed around with the script until I got to the one I originally posted. But its all working now thanks to that eagle eye on cooldown being under the seedage1 statement.