Help, my remote event fires twice and with every remote event it fires another time

I don’t know how to turn this into word form, but when you attack once it works alright, however if you still hold LMB after the 1st attack the 2nd attack fires twice. and with the 3rd attack it fires 3 times and so on. I don’t want this and I want to avoid this, please help!

Gif showing the prints.

The local script.

local rs = game:GetService("ReplicatedStorage")
local uis = game:GetService("UserInputService")
local tween = game:GetService("TweenService")
local attackfolder = rs.AttackFolder
local anims = script.Parent.Folder

local attackanim = anims.Attack1
local idle = anims.Idle

local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()
local char = plr.Character

local hum = char:FindFirstChild("Humanoid")
local attack = hum:LoadAnimation(attackanim)
local hrp = char.HumanoidRootPart
local animate = char:WaitForChild("Animate")

if animate then
	animate.idle.Animation1.AnimationId = idle.AnimationId
	animate.idle.Animation2.AnimationId = idle.AnimationId
end

local db = {
	["Global"] = false;
	["Basic"] = false;
	["E"] = false;
	["R"] = false;
	["F"] = false;
}

local MouseDown = false

uis.InputBegan:Connect(function(i,gp)
	if i.UserInputType == Enum.UserInputType.MouseButton1 and not gp then
		MouseDown = true
		
		while MouseDown == true do
			if db["Global"] == false and db["Basic"] == false then

				attack:Play()
				
				db["Global"] = true
				db["Basic"] = true
				
				attack.KeyframeReached:Connect(function(name)
					if name == "StartAttack" or name == "AttackEnd" then
						print(name)
						
						attackfolder.Attack1:FireServer(name)
					elseif name == "AnimEnd" then
						print(name)
						local dbc = coroutine.create(function()
							task.wait(.3)
							db["Global"] = false
							task.wait(.7)
							db["Basic"] = false
						end)
						
						coroutine.resume(dbc)
					end
				end)
			end
			task.wait()
		end
	end
end)

uis.InputEnded:Connect(function(i,gp)
	if i.UserInputType == Enum.UserInputType.MouseButton1 and not gp then
		MouseDown = false
	end
end)

The Serverscript.

local rs = game:GetService("ReplicatedStorage")
local attackfolder = rs.AttackFolder

attackfolder.Attack1.OnServerEvent:Connect(function(user,keyframe)
	local char = user.Character
	local hum = char:WaitForChild("Humanoid")
	local hitbox = script.Parent.Hitbox:Clone()
	
	if keyframe == "StartAttack" then
			local damage = 30
			
			hitbox.Parent = script.Parent.Blade
			hitbox.Position = script.Parent.Blade.Position
			
			local hitanti = {}
			if keyframe == "StartAttack" then
				hitbox.Touched:Connect(function(hit)
					if hit.Parent.Name ~= char.Name and hit.Parent:FindFirstChild("Humanoid") and not table.find(hitanti,hit.Parent.Name) then
						local hum = hit.Parent.Humanoid
						hum:TakeDamage(damage)
						table.insert(hitanti,hit.Parent.Name)
					end
				end)
			end
	elseif keyframe == "AttackEnd" then
		hitbox:Destroy()
	end
end)```
2 Likes

I think you should take rest and try again next time. :sweat_smile:

maybe :-

Client :

  1. remove while MouseDown == true do
  2. put attack.KeyframeReached outside of uis.InputBegan

Server :

From here
elseif keyframe == "AttackEnd" then hitbox:Destroy() end
Change to
elseif keyframe == "AttackEnd" then script.Parent.Blade:FindFirstChild("Hitbox"):Destroy() end
@ GerudoLlnk
I believe the only only way you need to think right now is just to disconnect attack.KeyframeReached event if you still want to use while loop. It will get double every time debounce end.

that’s all I can help :slightly_smiling_face:

2 Likes

How can i make it repeat when the player holds it then? I tried to do that here however I encountered a problem that doubles event firing.

(btw, thanks for giving me the solution :3)

2 Likes

Might help other users who see this thread if you explain how and what helped you reach the solution.

2 Likes