Tool runs multiple times when unequipped and equipped + Other tools not working when using one tool

The tool works fine at first, but when I unequip it and equip it again it runs twice, if i do it again it runs thrice and etcetera.
And also, if I use one tool, the other tools won’t work.

The local script is located in GameGui.
This is the local script:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local LocalPlayer = Players.LocalPlayer

local events = ReplicatedStorage:WaitForChild("Events")
local weaponEvent = events:WaitForChild("Weapon")

LocalPlayer.Character.ChildAdded:Connect(function(object)
	if object:IsA("Tool") then
		local currentTool = object
		
		local debounce = false

		if currentTool ~= nil then
			currentTool.Activated:Connect(function()
				if debounce == false then
					debounce = true
					weaponEvent:FireServer(currentTool)
					task.wait(1)
					debounce = false
				end
			end)
		end
	end
end)

This is the script that recieves the event:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local events = ReplicatedStorage:WaitForChild("Events")
local weaponEvent = events:WaitForChild("Weapon")

local weapon = {}

local equippedEvents = {}

weaponEvent.OnServerEvent:Connect(function(plr, tool)
	local debounce = false
	print(tool)
	if tool then
		if not equippedEvents[plr] then
			-- Connect the Equipped event for this player
			equippedEvents[plr] = true
			tool.Equipped:Connect(function()
				if tool then
					local attackAnim = tool:WaitForChild("Animations"):WaitForChild("Attack")
					local config = tool:WaitForChild("Config")
					local handle = tool:WaitForChild("Handle")
					local trail = handle:FindFirstChild("Trail")

					--[Types]

					local function MeleeAttack()
						plr.Character.Humanoid:LoadAnimation(attackAnim):Play(0.1, 1, attackAnim.Speed.Value)
						if trail then trail.Enabled = true end
						spawn(function()
							task.wait(1/attackAnim.Speed.Value/5)
							if tool then
								for i, object in pairs(workspace.Mobs:GetChildren()) do
									if object:IsA("Model") and object:FindFirstChild("HumanoidRootPart") and object:FindFirstChildOfClass("Humanoid") then
										if (handle.Position - object.HumanoidRootPart.Position).Magnitude < config.Hitbox.Value then
											object.Humanoid:TakeDamage(config.Damage.Value)
										end
									end
								end
								if trail then trail.Enabled = false end
								task.wait(1/attackAnim.Speed.Value/1.25)
							end
						end)
						task.wait(config.Cooldown.Value)
						debounce = false
					end

					--[Types End]

					tool.Activated:Connect(function()
						if debounce == false then
							debounce = true
							if config:WaitForChild("Type").Value == "Melee" then MeleeAttack()
							end
						end
					end)
				end
			end)
		end
	end
end)

return weapon

Can you send a video of this bug?

Because you making a event every time a tool enters your character.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.