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.
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.
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
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)
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?
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