basically, i'm trying to make it so when a bool value is true, it waits 5 seconds and then continues what the tool was going to do, but when its false, it instantly breaks and stops what it was going to do, but i just cant figure it out, it either does it instantly or doesn't do it at all, instead of waiting 5 and then doing it and breaking, any fixes? this is my code
holding.Changed:Connect(function() if holding.Value == true then for i = 0, 5 do if holding.Changed then print("ended prematuraly") return else print("healing in progress") wait(1) end end end
after all the ends the "if holding.Value == true then" continues and heals the player
The way i'd probably go about it is a loop like this
local bool = Instance.new("BoolValue") -- whatever bool you are tracking local wasFalse = bool.Value -- temporary value for i=1,5 do wait(1) wasFalse = wasFalse and bool.Value end if wasFalse then -- your healing code end
The way this works is if bool
is true then wasFalse
is just set to true, but if bool
is false wasFalse
gets set to false. Since wasFalse
is now false it cannot be true again because we use an and
statement.
Of coarse one problem is that if the value is set to false in between checks then it wont register, you can fix this by increasing the check interval.
Couldn't think of a good way to explain it, im happy to answer any questions.