Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

[SOLVED]Why wont my leaderstat freeze when it goes over the limit?

I am trying to make Rage AKA Stat lower down to the value of Max Rage AKA limit and then make Rage = MaxRage. If you can fix this simple error, I will be very happy.

P = game.Players.LocalPlayer
while true do
	wait()
if P.bin.Rage.Value > (P.bin.MaxRage.Value) then
	P.bin.Rage.Value = P.bin.MaxRage.Value
end

2 answers

✓ Accepted answer

Instead of wait(), make the script check the value when the value itself declares it has changed. All Instances in Roblox contain a Changed event, which fires whenever a Property of the Instance it fired from changes. Supposing you are using an IntValue or a NumberValue, you can use said Changed event to modify the value.

Here's the fixed portion of the script:

local P = game.Players.LocalPlayer -- You should probably change this to a local variable for more effeciency, as you don't have it inside any statement/event/etc but you have it at the very top of the script.
P.bin.Rage.Changed:connect(function(rage) --rage, in this case, is P.bin.Rage.Value
if rage > P.bin.MaxRage.Value then rage = P.bin.MaxRage.Value end --Parenthesis is not necessary
end)

EDIT: Shoot, i've been ninja'd by a moderator. The solution i bring you is more effecient than his because, instead of inspecting the amount of rage every time wait() is invoked, it inspects the value every time it is declared that it has changed. It should help run the game faster if you are gonna have many scripts in there.

His solution fixes your script and he is actually right on that - you are only using one end for two statements. Putting wait() in place of true when your while statement has a wait() at the beginning also improves effeciency - when this happens, the while statement is invoked every time the script has waited for the duration of wait().

RE-EDIT: Welp, guess the mod's answer got accepted. And i thought i was the one who got this easy one first :(

At least now i know there actually are people willing to help here in Scripting Helpers.

RE-RE-EDIT: Hey, my answer got accepted! Awesome!

The answer is simple, you are missing an end statement to close the loop. In addition, you could also shorten the script a tiny bit by replacing while true do with while wait() do and then removing line 3. The reason why this works is because the game knows that if it sees a wait there, it will run its wait function and then return a true value after the wait ends.

P = game.Players.LocalPlayer
while wait() do
	if P.bin.Rage.Value > (P.bin.MaxRage.Value) then
		P.bin.Rage.Value = P.bin.MaxRage.Value
	end
end

This discussion is preserved as it was. New answers, comments, and votes are closed.