Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Remote function fires multiple times?

So I made a tool that lets my avatar punch and do damage, which I connected through a remote event. The problem occurs whenever I equip the tool multiples times. So if I already equipped the tool the third time, the remote event fires three times, so the punch makes three times as much damage. I don't really know how to solve this, so help would be very appreciated.

Code from the client side:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local tool = script.Parent
tool.RequiresHandle = false
local debounce = false
 
tool.Equipped:Connect(function()
	    debounce = true
		Mouse.Button1Down:Connect(function()
			if debounce == true then
			game.ReplicatedStorage:WaitForChild("PunchEvent1"):FireServer()
			print("fired")
		end
	end)
end)

tool.Unequipped:Connect(function()
	debounce = false
end)

Code from the server side:

local Cooldown = false
local CanDamage = false
local Combo = 1
 
--Edit these variables
local Delay = .35
 
game.ReplicatedStorage:WaitForChild("PunchEvent1").OnServerEvent:Connect(function(Player)
print("received")
if Cooldown then return end

 
spawn(function()
    Cooldown = true
    wait(Delay)
    Cooldown = false
end)
 
local Swoosh = Instance.new('Sound',Player.Character.HumanoidRootPart)
Swoosh.SoundId = 'rbxassetid://231731980'
Swoosh.Volume = .2
Swoosh.EmitterSize = 100
 
local rightPunch = Instance.new("Animation")
rightPunch.AnimationId = "rbxassetid://3750767531"
local rightTrack = Player.Character.Humanoid:LoadAnimation(rightPunch)
 
local leftPunch = Instance.new("Animation")
leftPunch.AnimationId = "rbxassetid://3796677804"
local leftTrack = Player.Character.Humanoid:LoadAnimation(leftPunch)
 
local Finisher = Instance.new("Animation")
Finisher.AnimationId = "rbxassetid://3796674078"
local finalKick = Player.Character.Humanoid:LoadAnimation(Finisher)

 
if Combo == 1 then
rightTrack:Play()
Swoosh:Play()
local Dmg = script.RightArm:Clone()
Dmg.Parent = Player.Character["Right Arm"]
Dmg.Disabled = false
wait(Delay)
Dmg.Disabled = true
Dmg:Destroy()
Combo = 2

 
else if Combo == 2 then
leftTrack:Play()
Swoosh:Play()
local Dmg = script.LeftArm:Clone()
Dmg.Parent = Player.Character["Left Arm"]
Dmg.Disabled = false
wait(Delay)
Dmg.Disabled = true
Dmg:Destroy()
Combo = 3
 
elseif Combo == 3 then
rightTrack:Play()
Swoosh:Play()
local Dmg = script.RightArm:Clone()
Dmg.Parent = Player.Character["Right Arm"]
Dmg.Disabled = false
wait(Delay)
Dmg.Disabled = true
Dmg:Destroy()
Combo = 4

 
else if Combo == 4 then
leftTrack:Play()
Swoosh:Play()
local Dmg = script.LeftArm:Clone()
Dmg.Parent = Player.Character["Left Arm"]
Dmg.Disabled = false
wait(Delay)
Dmg.Disabled = true
Dmg:Destroy()
Combo = 5
 
else if Combo == 5 then
finalKick:Play()
Swoosh:Play()
local Dmg = script.Finisher:Clone()
Dmg.Parent = Player.Character["Left Arm"]
Dmg.Disabled = false
wait(Delay)
Dmg.Disabled = true
Dmg:Destroy()
Combo = 1
               
                end
            end
		end
    end
end)

Example of the output: (Everytime I equipped the tool, I let my avatar punch) fired received fired (x2) received (x2) fired (x3) received (x3) fired (x4) received (x4) fired (x5) received (x5)

1 answer

✓ Accepted answer

Put Button1Down event outside the equiped event because every time player equips the tool it duplicates it. You can have variable "Equiped" that you will change with equiped event and in Button1Down event check if it's true.

1 comment

0

Thank you very much it works fine now.

This discussion is preserved as it was. New answers, comments, and votes are closed.