Click Detector Duplicating "Click" Function

Hello everyone, I’ve got a rather odd situation. I have a problem with ClickDetectors and them triggering twice, I’ve tried adding a dbounce clause and it still duplicates the expected result which in this case is a helicopter. When I press on the button to spawn said helicopter it is meant to clone one from ServerStorage into the game world at the position it was originally located at. My issue is when using :Clone() or even a print("Test") inside of the MouseClick function the intended output is duplicated.

As you can see here Console_Output the expected output “Cloned new” is duplicated. The object is cloned twice as well as seen here Cloned_Helicopters

I’ve searched through the Wiki as well as these forums for this particular issue and I’ve come up completely empty as to why a ClickDetector would duplicate the function contents leading to a duplicated result. I’m experiencing a similar issue with ClickDetectors that give players tools. Depending on the session it may only spawn once; however, a majority of the time it is duplicated twice. I was wondering if anyone has any insight into this issue if you’ve experienced it before.

Code for Spawn Button

This is located inside of a ServerScript in the spawn button part.

local ClickDetector = script.Parent:WaitForChild('ClickDetector')
local CanRegen = script.Parent:WaitForChild('CanRegen')
local HelicopterKit = game.ServerStorage.Helicopters.Plot_One:WaitForChild("Military_Helicopter")
local CurrentHelicopter --= HelicopterKit:Clone()

ClickDetector.MouseClick:Connect(function(Player)
	if script.Parent.Parent.Parent.isPurchased.Value == true and CanRegen.Value == true then
		if CurrentHelicopter ~= nil then
			CurrentHelicopter:Destroy()
			CurrentHelicopter = HelicopterKit:Clone()
			CurrentHelicopter.Parent = script.Parent.Parent.Parent.Parent.Parent.Parent.Helicopters
			CurrentHelicopter:WaitForChild('Required'):WaitForChild('DriverSeat'):WaitForChild('Heli_SControl').Disabled = false
			print("Destroyed old, and cloned new")
		else
			CurrentHelicopter = HelicopterKit:Clone()
			CurrentHelicopter.Parent = script.Parent.Parent.Parent.Parent.Parent.Parent.Helicopters
			CurrentHelicopter:WaitForChild('Required'):WaitForChild('DriverSeat'):WaitForChild('Heli_SControl').Disabled = false
			print("Cloned new")
		end
	end
end)

I’m sure it’s a really silly error but I can’t for the life of me figure it out, anyways, thank you in advance.

3 Likes

What if you try using FindFirstChild instead of WaitForchild inside If statement where you print “Cloned new”

EDIT: Does not fire on mouse released, I was wrong!

I’m not really sure what’s going on here, to be honest.

The wording on the wiki (see below) could be interpreted as “click detector fires when the player presses and when the player released the mouse”, or “click detector fires when the player presses and releases the mouse.”

So let’s just assume it will fire when the player both presses and releases the mouse :man_shrugging:

A simple solution would be like you mentioned, using a debounce. I would structure the code as follows, but feel free to write it differently as long as it has the desired effect.

--code
if debounce == false and script.Parent.Parent.Parent.isPurchased.Value == true and CanRegen.Value == true then
    debounce = true
    delay(2, function() debounce = false end)
    -- code

Hopefully this helps, I would try and test it out myself but I’m on mobile currently! Good luck :slight_smile:

2 Likes

This event runs on the left click of a mouse(or tap on mobile devices) & not on release.

1 Like

Ah, well, it was a good guess anyway :stuck_out_tongue_winking_eye:

2 Likes

But the debounce can for sure be helpful for issues or events fired 2 times.

1 Like

Never had this issue before. Do you connect to the event twice or modify the script in any way (e.g. Disabled)? ClickDetectors work fine for me as expected, connecting only once.

1 Like

Try running this code on a new place, perhaps your place is having issues

Now that you mention it being disabled, I realize that I’d left it enabled by default. This led to my unintended duplicated result, and I’m just dumbfounded I didn’t think to check that earlier. Although in my defense it was 3 AM. So I’d like to thank you for pointing that out because I really don’t think I would have remembered to check that this morning. In any case I’ve resolved my problem by rewriting it and not relying on disabling / enabling the script.

I’d also like to thank everyone else for their replies, it was all useful advice that I may end up using.

1 Like