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

how would i make it so while a bool is true, the tool waits 5 and then activates?

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

1 answer

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.

11 comments

0

well i just tested it and what its doing is waiting 5 seconds and working, but if i stop holding click, it keeps going, what i'm trying to do is make it so when you stop holding click, it returns back to waiting for the function to be called and doesnt execute the healing code

0

You shouldnt need to make it stop since, I assume, you put it in an event like you did with your own code.

0

If you didnt I probably should have mentioned that

0

basically how my code runs is, there is an event that fires everytime an input starts/ends using userinputservice, and detects if the player is holding mouse1, and then turns the bool on/off

0

the bool is a BoolValue object correct?

0

yeah its the same, do you want me to send you the client/server parts of the bool on/off switch?

0

I think I know what is going wrong, let me fix the code

0

I just added the proper stuff for the BoolValue, now all that you need to do is replace "Instance.new("BoolValue")" with whatever BoolValue you are using

0

still does the same thing, very strange

0

Yea, ill have to test it myself when i get the chance.

0

alrighty then, tell me when you test it and if theres anything wrong on your end

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