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)
“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.
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”.
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)
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…