How do I prevent my remote event from firing twice?

I have a local script which fires a remote event when clicked, and the server script recieves it. However, I was debugging and noticed it printed twice when the remote event was fired.

Local Script:

local event = game.ReplicatedStorage.event

script.Parent.Activated:Connect(function() 
	event:FireServer()
	print("Fired!") --this only prints once
end)

Server Script:

local event = game.ReplicatedStorage.event

event.OnServerEvent:Connect(function()
	local cooldown = false
	if cooldown == false then
		cooldown = true
		
		--my code
		
		print("Recieved") --this always prints twice
		task.wait(1)
		cooldown = false
	end

end)

1 Like

“I have a local script which fires a remote event when clicked, and the server script recieves it. However, I was debugging and noticed it printed twice when the remote event was fired.”

I think that might be due to using the “Activated” event…
Not entirely sure though, if it is then use a different event.
Or you could probably utilise a boolean.

I tried using MouseButton1Click and got the same result, and I don’t think any other events apply

1 Like

Just tested your previous code and it works fine…

1 Like

Right, it works fine, but it’s either firing twice or being recieved twice based on the fact that the server script gets printed twice

1 Like

Can you send a file of the world? It may be working but something is calling the remote twice.

1 Like

I don’t think that’s necessary. The two scripts I provided are the only ones interacting with the event

If it isnt necessary then figure out why its being called twice.
If your tool calls it once then somthing else is calling it.

Also fix your cooldown, Should be like this

local event = game.ReplicatedStorage.event
local cooldown = true
event.OnServerEvent:Connect(function()
	if cooldown then
		cooldown = false
		
		--Insert Code.
		
		print("Debounced")
		task.wait(1)
		cooldown = true
	end

end)

Reason being is that everytime you call the remote itll set cooldown to false meaning there is no “Cooldown”.

You may be reviewing from the Output, which prints on the client and the server. Which may appear to print twice, but it only print’s once.

That makes sense, I am using the output. But would that be the case for just server scripts? I have noticed getting 2x prints

image
Testing it and it works fine. Personally I think something else is calling it or it is your Debounce.

EDIT: Spamming it
image

Or he could use a public value for testing, just for further knowing if it does go through…

Local

local replicatedStorage = game:GetService("ReplicatedStorage")

local event = replicatedStorage.event
local tool = script.Parent

tool.Activated:Connect(function() 
	event:FireServer("ToolClicked")
	print("Clicked")
end)

Server

local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")

local event = replicatedStorage.event

players.PlayerAdded:Connect(function(player)
	local boolean = Instance.new("BoolValue", player)
	boolean.Name = "Cooldown"
	boolean.Value = false
end)

event.OnServerEvent:Connect(function(player, action)
	if player and action == "ToolClicked" then
		local cooldown = player:FindFirstChild("Cooldown")
		if cooldown.Value == false then
			cooldown.Value = true
			print("Released")
			task.wait(3)
			cooldown.Value = false
		end
	end
end)

I guess what ever works for him…

1 Like

It’s not a tool, it’s a textbutton

well it would basically be the same as:

local replicatedStorage = game:GetService("ReplicatedStorage")

local event = replicatedStorage.event
local button = script.Parent

button.MouseButton1Click:Connect(function()
	event:FireServer("Clicked")
	print("Clicked")
end)

And also changing something in the server side of things…

if player and action == "Clicked" then

Would it be possible to remove the player from use in that as I want to use it to access leaderstats at some point

Is it a button? In that case, you don’t need a local script, since MouseButton1Click fires on the server.

Can you elaborate?
So that I can get a better understanding on what you’re talking about…

On the server script I want to use player in the OnServerEvent function which would allow me to access the leaderstats and such. So is the use of player in the script you wrote necessary?

Yeah because you’re checking to see who fired the event if you’re using
an if statement for checking the argument in the OnServerEvent,
and also if you’re wanting to give rewards to the player that fired the event…

Could I potentially use plr then?