So I have a NumberValue that is called Health and I want the max health to be 1000 and the min 0, if at any point it is under 1000 I want it to regenerate back to 1000 with a random interval of 1 to 50 but after 1000 it continues forever? Help! Here is what I had:
script.Parent.Value = 1000 while script.Parent.Value <= 1000 do repeat script.Parent.Value = script.Parent.Value + math.random(1,50) wait(math.random(1,5)) until script.Parent.Value == 1000 end
Because you are seeing if it equals 1000. Since you are doing math.random(1,50) there's a chance that it could pass 1000. To fix that do script.Parent.Value >= 1000
Also you don't need the repeat until in there.
You can just do this
script.Parent.Value = 1000 --Not sure what's this for while script.Parent.Value <= 1000 do script.Parent.Value = script.Parent.Value + math.random(1,50) wait(math.random(1,5)) end script.Parent.Value = 1000 --This will set it to 1,000 afterwards if you want it too.
Never check if a number is exactly equal to something unless you know for certain that it will be. In this case, adding a random amount can very easily go over 1000 without reaching it.
The easiest way to make this work as expected is to connect the Changed
event of the IntValue (I presume it is an Int and not a NumberValue) and have it set some 'regen' variable for use in an infinite loop:
local hp = script.Parent local regen = false hp.Changed:connect(function() regen = hp.Value < 1000 end) while wait() do if regen then local healing = math.random(50) if healing > 1000 - hp.Value then --So we don't accidentally overheal. healing = 1000 - hp.Value end hp.Value = hp.Value + healing wait(math.random(5)) end end