Alternative to using wait()?

I have a working script that is basically just a tool that plants a bomb. as you can see I use wait() to tell the bomb when to change color and when to explode. But I’m aware using wait() is a malpractice. Unfortunately, i’m an amateur scripter and i don’t know my options. Can anyone give me some pointers?

local Tool = script.Parent
local bomb = Tool.Handle
Tool.Activated:Connect(function()
	local bomb_decoy = bomb:Clone()
	bomb_decoy.CanCollide = true
	bomb_decoy.Parent = workspace
	bomb_decoy.Anchored = false
	local x = 5
	while x > 0 do
		wait(0.5)
		bomb_decoy.BrickColor = BrickColor.new("Really red")
		wait(0.5)
		bomb_decoy.BrickColor = BrickColor.new("Really black")
		x = x - 1
	end
	if x == 0 then
		bomb_decoy.Transparency = 1
		bomb_decoy.Anchored = true
		local explosion = Instance.new("Explosion")
		explosion.Parent = workspace
		explosion.Position = bomb_decoy.Position
		explosion.BlastPressure = 100000
		bomb_decoy:Destroy()
	end
end)

task.wait. sssssssssssssssssssss

2 Likes

task.wait()
game[“Run Service”].Heartbeat:Wait()
game[“Run Service”].Stepped:Wait()
game[“Run Service”].RenderStepped:Wait()

[The last 3 ones are useful depending on what you want to use them on].

1 Like

The use here is negligible, but it’s possible to avoid task.wait by using task.delay.

local function bombTick(bomb, ticks)
	bomb.BrickColor = BrickColor.new(ticks%2 == 0 and "Really red" or "Really black")
	
	if 0 < ticks then
		ticks -= 1
		return task.delay(0.5, bombTick, bomb, ticks)
	end
	
	local explosion = Instance.new("Explosion")
	explosion.Position = bomb.Position
	explosion.BlastPressure = 100000
	explosion.Parent = workspace
	
	bomb:Destroy()
end

bombTick(bomb_decoy, 10)
4 Likes

thanks to all the responses. I was needing the insight because i was going to have a lot of these ticking bombs, and i need the countdown to be psuedo real-time. i guess i should have mentioned that in the original post thanks again !

I would just track time with tick() as long as it is all done either server/clientsided

1 Like

I have custom wait that can go way lower and give way accurate results than task.wait().
It can currently do 0.001 wait and be more accurate than task.wait().
results like this:
0.001004878788
0.00108575755
0.00109575775
0.00100487575

planning to open-source it? just wamt to see it

I don’t feel like it’s worth sharing its currently just for custom coroutine but can be turned into a custom wait function.

1 Like

Also, it will get those times only as server script, not as client script.

1 Like