Issue understanding debounces

My code works but I don’t understand how my debounce works. I’ve tried to replace the remote event with a remote function and send data to the event but it didn’t work compared to this code. How does the debounce also pause the script that fires the local script? How does this work?

Local script that moves the trash can:

local debounce = false
game.ReplicatedStorage.Events.TrashEvent.OnClientEvent:Connect(function(touch)
	if not debounce then
		debounce = true
		for i = 1,30 do
			task.wait()
			local flap = touch:GetPivot()
			touch:PivotTo(flap * CFrame.Angles(0,0,math.rad(3)))
		end
		task.wait(3)
		for i = 1,30 do
			task.wait()
			local flap = touch:GetPivot()
			touch:PivotTo(flap * CFrame.Angles(0,0,math.rad(-3)))
		end
		debounce = false
	end
end)

Server script that fires the local script(there is also a remote function to identify an instanced tool):

local touch = workspace.Trash.Layer
touch.ClickDetector.MouseClick:Connect(function(plr)
	game.ReplicatedStorage.CreateWeldFunction.OnServerInvoke = function(plr,name)
		if name == "Food" then
			return true
		else
			return false
		end
	end
	plr.Backpack:ClearAllChildren()
	game.ReplicatedStorage.Events.TrashEvent:FireClient(plr, touch)
end)

It doesn’t pause the code from the local script.
If the local script ever fired the events multiple times, the server will run all of them.
Just go through your code line by line. 90% of scripting is about algorithm.

1 Like

Oh I see thank you.ddadadsaddsadasdadasdadasd

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