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

Use something else rather than Wait()?

So, im doing a script for a logic gate, that the player will be able to choose from delay and hold (i dont know if i will add more later). And the code is the following:

	if FT == true then -- FT is the status of the input (true or false)
		ACTIVE = true -- prevents plr from changing the timer mid activation
		IN.BrickColor = BrickColor.new("Bright blue") -- changes INPUT color
		--wait(TOTALTIME) -- error here!!
		--note: TOTALTIME is in seconds, by multiplying minutes by 60
		OUT.BrickColor = BrickColor.new("Bright blue")
		status[INDEXOUT1] = true
		
	else
		ACTIVE = false -- lets the player change the timer again
		IN.BrickColor = BrickColor.new("Black") -- changes back color
		OUT.BrickColor = BrickColor.new("Black")
		status[INDEXOUT1] = false
	end
	
	

but everytime the script reads wait(TOTALTIME) it will stop everything and wait untill that is done i tried something else like:

while TIME > 0 do
	wait(0.001)
	TIME = TIME - 0.001
end
if TIME <= 0 then
	TIMEOVER = true
else
	TIMEOVER = false
end

but its not decreasing correctly, if there is no wait() it does it instantly. but if there is a wait() it does it super slowly, not on the correct time. I have no idea of how to fix this, if someone could help me i would appreciate it alot!

3 comments

0

im trying to use delay() to see if it works, still testing it tho

0

You really need to use wait() (or task.wait() rather) in a loop otherwise it will break your game.

0

Also can you please explain more of what you're trying to do? It's confusing for me..

1 answer

All the code is being run on the same thread, which is why the loop stops everything else until it is finished. You can run the while loop on a different thread using a coroutine

Perhaps you can also try using the Heartbeat with the Wait function, which will wait for the event to fire, which will be every frame.

coroutine.wrap(function()
	while TIME > 0 do
		game:GetService("RunService").Heartbeat:Wait() 
		TIME = TIME - 0.001
	end
end)()

if TIME <= 0 then
    TIMEOVER = true
else
    TIMEOVER = false
end

2 comments

0

Isn't task.wait() equivalent to Heartbeat:Wait()? Perhaps you could try that instead.

0

Oh. You are correct, I was not aware that they were the same, thanks.

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