RemoteEvent from client to server fires twice

  1. What do you want to achieve? Keep it simple and clear!
    Make a tool which can give other player item after you hovered mouse on him and clicked.

  2. What is the issue? Include screenshots / videos if possible!
    Issue is that for some unknown reason for me, server receives event fire twice and because of that gives player two tools.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I checked almost every DevHub post about remote event firing twice but most solutions didn’t work such as: adding debounce, using additional remote event.
    Only close one was that event was fired from two clients but I did not figure out is it the actual issue for me

Local script:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local tool = script.Parent.Parent
local equipped = false
local cangive = false
local debounce = false

local event = game.ReplicatedStorage:WaitForChild("GivingEvent")
local gui = game.ReplicatedStorage:WaitForChild("Bar Features"):WaitForChild("GivingGUI")

tool.Equipped:Connect(function()
	equipped = true
	gui:Clone().Parent = player.PlayerGui
end)


tool.Unequipped:Connect(function()
	equipped = false
	player.PlayerGui:FindFirstChild(gui.Name):Destroy()
end)

game:GetService("RunService").RenderStepped:connect(function()
	if mouse.Target ~= nil and equipped then
		if mouse.Target.Parent:FindFirstChild("Humanoid") then
			player.PlayerGui:FindFirstChild(gui.Name).TextLabel.Text = "Giving to: " .. mouse.Target.Parent.Name
			cangive = true
		else
			player.PlayerGui:FindFirstChild(gui.Name).TextLabel.Text = "Giving to: "
			cangive = false
		end
	end
end)

tool.Activated:Connect(function()
	if cangive then
		if debounce then return end
		debounce = true
		print("fired")
		local giveplayer = game:GetService("Players"):GetPlayerFromCharacter(mouse.Target.Parent)
		event:FireServer(giveplayer)
	end
end)

Server script:

local event = game.ReplicatedStorage:WaitForChild("GivingEvent")
local item = game.ServerStorage:WaitForChild("Coffee")
local tool = script.Parent.Parent

event.OnServerEvent:Connect(function(player, giveplayer)
	print("received")
	item:Clone().Parent = giveplayer.Backpack
end)

Output:

-- fired  -  Client - LocalScript:38

-- received  -  Server - Script:7
-- received  -  Server - Script:7

Thanks a lot for helping me in this issue, I am just beginner at scripting and this issue also stopped me in some other scripts. Cheers

Is the localscript an “actual” localscript (as in its ClassName is local script) or is it a script with its context set to client?

Yea, it is a local script. Definitely

OH CRAP, I just noticed I messed up the desc! I edited it right now as it might have confused you, I am so sorry.

Just for clarity, did the message fired get printed into the console twice?

Thats the thing that it doesnt, it prints only once but received prints twice

Are you sure you don’t have duplicate scripts in the game?

I am sure I dont, only one script in tool, thats it

Have you tried to complete the debounce like this:

tool.Activated:Connect(function()
	if cangive then
		if debounce then return end
		if not debounce then
        debounce = true
		print("fired")
		local giveplayer = game:GetService("Players"):GetPlayerFromCharacter(mouse.Target.Parent)
		event:FireServer(giveplayer)
        wait(1)
        debounce = false
        end
	end
end)

Hm. I will try it, I am unfortunetaly not on pc right now so I will reply again in like half a hour if it worked, thanks in advance!

Still didn’t work, I might know why it happens but how obvious, I don’t know why it happens. So I have guess that event just detects it being fired two times, one is actual player and second one can be maybe startergear? As I put the tool into it, can it affect code somehow?

I mean that I don’t know why it receives it twice and my guess is that something else from player client fires it at same time as player uses tool

Just to clarify, your script does NOT have client selected?
image

image
Yea, it doesnt

I’m talking about the script you’re firing the remote event from.

I am firing it from local script.

Are you sure your script isnt duplicated, or you have a plugin thats duplicating it?
Try clicking on each “recieved” in console and see which script it brings you too

Why are you checking debounce twice lol, its already checked on the above?

Alright, you were right. So it fires event two times from each of player. Maybe it’s because I put it into starterpack and every player has it?
image

Right now, I just have no idea why it fires it from other player through game.Players…