Script does not work

Hello Developers,

I made a leaderstasts script that increases level every 80 seconds but the problem is that it does not stop at the maximum value.

Can you help?

Here is my script:

	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local level = Instance.new("NumberValue")
	level.Name = "Level"
	level.Parent = leaderstats
	
	level.Changed:Connect(function(value)
		wait(80)
		level.Value = level.Value +1
		if value > 100  then
			level.Value =100
		end
	end)
	
	
	
end)
3 Likes

It’s not stopping at the maximum value because the conditional statement isn’t checking the most updated version of level.Value; it’s referring to the one returned from the Changed event before you increased the leaderstat.

To resolve that, update the conditional statement to check its current value:

level.Changed:Connect(function()
    task.wait(80)

    level.Value += 1

    if level.Value > 100 then
        level.Value = 100
    end
end)
2 Likes

Use a while loop instead, also instead of comparing value you should compare level.Value:

task.spawn(function()
	while task.wait(80) do
		level.Value += 1
		if level.Value >= 100 then break end
	end
end)
1 Like

Thank you so much it worked!!!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.