Trashing tools script

I’m trying to make a model that pivots the CFrame of a model to make a smooth opening of the top of a garbage/trash container and once this model latch is clicked, everything in the player’s backpack is destroyed. The problem is when I click on the latch it has no cooldown/debounce so I can click it so that it can go in 360 degrees. I use a local script to rotate the latch, meaning when I click the part I’m using a remote event that fires the client. I’ve tried using remote functions, remote events, and moving around debounces but nothing has come close to working. I’ve thought of using InvokeClient, but I heard it has had a bad reputation.

Questions:
-How can I sync this local script and server script so that I can create a client-side debounce?
-Should I use invoke client in this case?

This script is the script that moves my latch/garbage hood and puts its back down:

game.ReplicatedStorage.Events.TrashEvent.OnClientEvent:Connect(function(touch)
	local debounce = false
	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
		task.wait(2)
		debounce = true
	end
end)

This is the server script that creates an event when the latch is clicked. I have a remote function to gain data of an instanced tool from another script:

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)
1 Like

This is simple, right now the way you have debounce, it makes a new debounce variable every time the event is fired. Instead, keep the variable outside of the function, like this:

local debounce = false

-- game.ReplicatedStorage...
3 Likes

It works, thanks! [character limit]

2 Likes

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