Remote event firing multiple times

Freud.MouseButton1Click:Connect(function()	-- Player clicks on billboardGUI button
	Buy.MouseButton1Click:Connect(function()    -- On buy button pressed, function executes
		local PurchaseRM = game.ReplicatedStorage.RemoteEvents.Merchants.PurchaseCargo
		local cargo = cargoName
		PurchaseRM:FireServer(cargo)
	end)
end)

The function within the function fires multiple times, thus the remote event also firing multiple times. I suspect it’s because of the function being inside of another MouseButton1Click function. However, I’m unsure of how to fix this.

And no, I’ve already tried adding debounces to both the local and server script.

How long did you make the debounce?

I believe it is because the Buy button is getting connected to a MouseButton1Click everytime someone clicks the button named “Freud”, to remove this behavior, I would do this:

Assign the Buy button connection to a Variable, then you will have to Disconnect that connection when the person clicks on the buy button. So basically just refactoring some part of your code like this should help:

Freud.MouseButton1Click:Connect(function()	-- Player clicks on billboardGUI button
    local BuyButtonConnection --Empty Variable for assigning the connection.
	BuyButtonConnection = Buy.MouseButton1Click:Connect(function()    -- On buy button pressed, function executes
        BuyButtonConnection:Disconnect() --Disconnects The Connection
		local PurchaseRM = game.ReplicatedStorage.RemoteEvents.Merchants.PurchaseCargo
		local cargo = cargoName
		PurchaseRM:FireServer(cargo)
	end)
end)

You could also have the Disconnect code in a close button, if you have one, you probably got the idea now.

10 Likes