Function / RemoteEvent running more each time it's being executed

I wanted to make a weapon system.
When I use it for the 1st time it’s fine, but it fires more each time it’s getting executed and I cannot figure out why.

Here is the script:

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

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

local weapon = {}

weaponEvent.OnServerEvent:Connect(function(plr)
	local debounce = false
	local tool = plr.Character:FindFirstChildOfClass("Tool")
	if tool then
		tool.Equipped:Connect(function()
			tool = plr.Character:FindFirstChildOfClass("Tool")
			if tool then
				local attackAnim = tool:WaitForChild("Animations"):WaitForChild("Attack")
				local config = tool:WaitForChild("Config")
				local handle = tool:WaitForChild("Handle")

				--[Types]

				local function MeleeAttack()
					plr.Character.Humanoid:LoadAnimation(attackAnim):Play(0.1, 1, attackAnim.Speed.Value)
					task.wait(config.Cooldown.Value/2)
					tool = plr.Character:FindFirstChildOfClass("Tool")
					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 < 3 then
									object.Humanoid:TakeDamage(config.Damage.Value)
								end
							end
						end
						print("a")
						task.wait(config.Cooldown.Value/2)
					end
					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)

return weapon

Try this and tell me if any error.

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)
  local debounce = false
  local tool = plr.Character:FindFirstChildOfClass("Tool")
  if tool then
    if not equippedEvents[plr] then
      -- Connect the Equipped event for this player
      equippedEvents[plr] = true
      tool.Equipped:Connect(function()
        tool = plr.Character:FindFirstChildOfClass("Tool")
        if tool then
          local attackAnim = tool:WaitForChild("Animations"):WaitForChild("Attack")
          local config = tool:WaitForChild("Config")
          local handle = tool:WaitForChild("Handle")

          --[Types]

          local function MeleeAttack()
            plr.Character.Humanoid:LoadAnimation(attackAnim):Play(0.1, 1, attackAnim.Speed.Value)
            task.wait(config.Cooldown.Value/2)
            tool = plr.Character:FindFirstChildOfClass("Tool")
            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 < 3 then
                    object.Humanoid:TakeDamage(config.Damage.Value)
                  end
                end
              end
              print("a")
              task.wait(config.Cooldown.Value/2)
            end
            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
1 Like

It’s fixed now, no errors show up, thanks!

No problem let me explain, Equipped event is being connected multiple times for the same player , Equipped need to check the flag before connecting the event i don’t know if you see what i mean or read my script you will see what i meaning :cool:.

1 Like

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