Events firing twice

Hey guys how do i stop this from happening:

LocalScript:

local contextac = game:GetService("ContextActionService")

script.Parent.Equipped:Connect(function()
	script.Parent.Setup:FireServer()
	local function inputHandler(actionName, inputState, inputObject)
		if inputState == Enum.UserInputState.Begin then
			if actionName == "Shot" then
				script.Parent.RemoteEvent:FireServer()
			end
		end
	end
	contextac:BindAction("Shot", inputHandler, false, Enum.UserInputType.MouseButton1)
end)

ServerScript:

script.Parent.Setup.OnServerEvent:Connect(function()
	print("Setup")
	
	script.Parent.RemoteEvent.OnServerEvent:Connect(function()
		print("Shot")
		end)	
end)

If you equip and unequip the tool “Shot” prints multiple times. How do i make sure it only fires once? Thanks.

2 Likes

Try Using a debounce on the Client

local denounce = false

script.Parent.Equipped:Connect(function()
if denounce == false then
debouce = true
	script.Parent.Setup:FireServer()
	local function inputHandler(actionName, inputState, inputObject)
		if inputState == Enum.UserInputState.Begin then
			if actionName == "Shot" then
				script.Parent.RemoteEvent:FireServer()
			end
		end
	end
	contextac:BindAction("Shot", inputHandler, false, Enum.UserInputType.MouseButton1)
debouce = false
end
end)

This doesnt help. It happens when I equip and unequip the tool. I think it has something to do with the RemoteEvent being inside another RemoteEvent but i dont know what to do to fix it.

You connect to the RemoteEvent’s OnServerEvent event again each time Setup is fired. So the second time you equip it will print twice, then the next time it will print three times, etc.

Define it once, outside of the setup event. Or disconnect it in response to an anti-setup event.

yeah this is my problem. How would i disconnect it?

Another event, probably called Clear or Reset or something, just like the setup one.

Store the connection in a variable, and disconnect with the clear event.

local connection

script.Parent.Setup.OnServerEvent:Connect(function()
	print("Setup")

    if connection and connection.Connected then
        connection:Disconnect()
    end
	
	connection = script.Parent.RemoteEvent.OnServerEvent:Connect(function()
		print("Shot")
		end)	
end)

script.Parent.Reset.OnServerEvent:Connect(function()
	print("Reset")

    if connection and connection.Connected then
        connection:Disconnect()
    end
    connection = nil
end)
8 Likes

Awesome, it looks to be working, could you give an example of defining the remote outside the event for the future

Maybe he should Unbind the mouse button when he unequips the tool, also?

script.Parent.UnEquipped:Connect(function()
     local contextac = game:GetService("ContextActionService")
     contextac:UnbindAction("Shot")
end)

Thing is, this is a simplified version of a gun script actually. I unbinded in the gun script, dont worry :slight_smile:

Sometimes the studio tends to bug out on me especially in team create it tends to make 2 copies and runs the code twice. I happen to encounter this bug if the studio is open for a long time and restarting the studio usually fixes it. I hope this will help you.

1 Like

Yeah i heard of this problem before. I already fixed it, but thanks for your input!