My part.Touched debounce is still allowing FireClient Event to happen twice

I am attempting to have items be picked up by a player. The item has a script that uses .touched with a debounce to fire a remote event to the player scripts to pick up the item and notify the player that they have done so. It appears that the debounce is working based on the output below as it only prints one time for the lines below:

23:51:56.514 true - Server - PickupDroppedItem:54
23:51:56.514 I touched the part! iter: 1 - Server - PickupDroppedItem:62

local Part = script.Parent
local debounce = true

local iter = 0


Part.Touched:Connect(function(other_part)
	print(debounce)
	if debounce == true then
		debounce = false
		iter = iter + 1

		local part_parent = other_part.Parent
		local humanoid = part_parent:FindFirstChildWhichIsA("Humanoid")
		if humanoid then
			print("I touched the part! iter: "..tostring(iter))
			local player = game.Players[humanoid.Parent.Name]
			PickupCollision:FireClient(player, Part)
		end

		task.wait(1)
		debounce = true
	end

	
end)

However after that I instantly get double outputs from the client side code as seen below even though I have attempted to add an additional debounce (can_touch) at this level as well. You can see from the times that they are hitting at the exact same time:

23:51:56.533 handling item collision of Wood - Client - PlayerCollision:68
23:51:56.533 handling item collision of Wood - Client - PlayerCollision:68

PickupCollision.OnClientEvent:Connect(function(part)
	if can_touch then
		can_touch = false
		warn("handling item collision of "..part.Parent.Name)
		repeat task.wait() until _G.pickup_item_and_persist ~= nil
		_G.pickup_item_and_persist(part)
		
		task.wait(1)
		can_touch = true
	end
end)

I have tried several things that I have found online from changing the debounce to a time related check and have quadruple checked that I don’t have duplicate code. I’m sure this is something blatantly obvious that I am just missing. Any help would be appreciated.

Thanks in advance!

1 Like

Make it so it can detect your FireClient Event and if it happens twice discard the second one

I genuinely don’t know what’s going on. You copied and pasted this right? My first thought was this but it doesn’t seem relevant to your problem. Could we get more context if possible?

1 Like

You may have a duplicate of the FireClient callback function. Try clicking on both client-sided warnings while testing to see the script from which they are called.

image
Something like this is always something you rule out first. if this is occurring then no matter how much debounces you put it won’t fix it. press Ctrl+Shift+F and look for parts of your code.
If you see something like this:


Go through them both and delete one.

1 Like

Instead of Part.Touched:Connect try Part.Touched:Once

For additional information requested in the comments:

There is only one instance of the script generated from what I can tell. I looked at that avenue, but it is possible I missed something. The flow is like this:

Player destroys something (ex: tree) that model drops items that contain a PickupDroppedItem script (the first code snipet from the original post) that is copied from the replicated storage for each dropped item. Once the player touches any of the dropped items it sends the FireClient function to another script called PlayerCollision (second code snipet above) that is stored in StarterCharacterScripts. The PlayerCollision script is what is running twice, but it looks like the PickupDroppedItem server script is only running once through warnings in the debug window:

image

the “I touched the part!..” print only happens one time while it does print out the true and falses for the debounce value several times, however it does the following code after the PlayerCollision script twice.

Thank you again for all the suggestions. Please let me know if there is something I am missing with the additional information I have provided.

Thank you for the suggestion. I couldn’t find the documentation for this does it trigger only once ever or is there a timeframe that you can give it for a cooldown before it can trigger again. I would need it to hit more than once in some cases (example: if a players inventory was full when they went to pick it up the first time). Would it be possible to use this in a way that solves that problem?

It only triggers once. After the event is fired it automatically disconnects the RBXScriptConnection.

The post below should help you understand it:

As for your current problem: I copied the code and could not replicate the issue.