I need help with alternatives to wait()

local s = script.Parent
local CF = CFrame.new
local F = false
local k = Enum.KeyCode.E 
local player = game:GetService("Players")

s.ClickDetector.MouseClick:Connect(function()
	s.ClickDetector.MaxActivationDistance = 0
	s.PotBreaking:Play()
	for b = 0,1,0.1 do
	for _, i in pairs(s:GetDescendants()) do
			if i:IsA("BasePart") then
				i.Anchored = false
				i.Transparency = b
				--wait(.01)
			end
		end
	end
end)

Hi i have made a simple script which basically makes a pot in game shatter into pieces and then disappears slowly over time as the transparency slowly fades it away however to do this i need a wait function to give the specified amount of time in which i want it to disappear however i have heard that wait() functions should be avoided and have tried to learn about task.delay and heartbeat but its confusing and it would be a great help if someone could explain it to me thanks.

Try task.wait() instead it’s simply the updated version of wait()

1 Like

If you want the blocks to fade away, you could use tween-service or make an updater function that updates all the bricks and is called before each frame is rendered (with RunService.RenderStepped).

I think if you want to just use wait for the sake of simplicity, you can do something like this:

for pos = 0, 1, 0.1 do
    -- Update all of the parts and change their transparencies
    for _, i in pairs(s:GetDescendants()) do
			if i:IsA("BasePart") then
				i.Anchored = false
				i.Transparency = pos
			end
		end
	end
    -- Wait for 0.1 seconds
    task.wait(0.1)
end

The important thing here is to wait outside of the loop where you update the properties of the parts, so that all the parts are updated and then you wait for a little bit before doing it again.

1 Like

I use wait() in most of my scripts … :fearful::exploding_head:

Same i have been using wait but i watched a youtube video saying it can interfere with the game.

1 Like

This is a great help but what does task.wait() do differently from wait()
itself also i haven’t started using tweenservices yet so i’m pretty clueless on that aspect.

task.wait is just more accurate than wait. For more information, you can check out this post: Task.wait(n) vs wait(n) - #3 by LucasMZ_RBX

task.wait should never be ‘avoided’, it has its own purpose and you should use it when you need to wait a certain amount of time. From what I remember, task.wait is recommended over wait since the latter throttles, and task.wait doesn’t.

If you need precise time difference and you’re stacking waits, you will need to create your own solution to compare the time it started with the existing time (like os.clock). This is because over time, it will lose precision.

Compare the delay of these three loops:

local waitTime = 5

-- Once
local init = os.clock()
task.delay(waitTime, function()
	warn("task.wait once: ".. (os.clock()-init))
end)

-- 50 times
task.spawn(function()
	local init = os.clock()
	for i = 1, 50 do
		task.wait(0.1)
	end
	warn("task.wait 50 times: ".. (os.clock()-init))
end)

-- Much more accurate
local init = os.clock()
while (os.clock()-5) < init do
	task.wait()
end
warn("task.wait, comparing time per heartbeat frame: ".. (os.clock()-init))

task.wait, comparing time per heartbeat frame: 5.007581199999549
task.wait once: 5.007693299998209
task.wait 50 times: 5.406994599998143

1 Like

If you want to avoid wait, then just RunService.HeartBeat, even though task.wait() is basically the same thing.

1 Like