Wait/Task.wait not working

Hey I’ve recently come back to roblox after 2 years and I’ve been doing good till I got here,

My wait script isn’t working no matter where I put it in the script (Client), this leads into a server event to add cash to a players account depending on the speed they are traveling.

I’ve tried putting it in almost every line using Task wait and wait but nothing so far, I assume this is something so simple that I am looking over but apparently I can’t catch it.

local bike = script.Parent.Bike.Value
local handler = bike:WaitForChild("add")
local values = script.Parent.Values

function sendCash(amt, plr)
	if bike and values.Parent.IsOn then
		print(amt,plr)
		--handler:FireServer(amt, plr)
	end
end

values.Velocity.Changed:Connect(function()
	task.wait(0.2)
	local spd = math.floor((10/12) * (60/88)*values.Velocity.Value.Magnitude)
	if spd >= 30 then
		local plr = game.Players.LocalPlayer
		local amt2 = spd/10*(spd/100+1)
		local amt3 = math.floor(amt2+0)
		local amt4 = math.clamp(amt3, 1, 65)
		sendCash(amt4,plr)
		task.wait(0.2)
	end
end)

image_2025-01-19_123215547

Too Much Yapping; Ain’t Reading Allat: Use a debounce pattern.

task.wait and wait are described as functions that “yield a thread”. It basically stops the current block of code from executing until a certain time passes. One thing you must be wary of, however, is that event methods (such as Velocity.Changed:Connect) create a new thread (represented by the function that you pass as an argument). In other words, when you use task.wait inside this function, you are yielding that, and only that thread. Code outside of it isn’t at all affected.

One more thing to keep in mind is that event connections (Velocity.Changed:Connect’s return value) cannot be interrupted and will run in parallel with the rest of the code, even if the code is currently processing a task.wait (yielding).

So, to work around all of that, a debounce is typically used.

2 Likes

Thank you for the input, worked.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.