local Players = game:GetService("Players") local RunService = game:GetService("RunService") local Workspace = game:GetService("Workspace") local ReplicatedStorage = game:GetService("ReplicatedStorage") local player = Players.LocalPlayer local character = player.Character or player.CharacterAdded:Wait() local myRoot = character:WaitForChild("HumanoidRootPart") local ATTACK_RANGE = 100 local COOLDOWN = 0.1 local ATTACK_TYPES = {"Attack", "HeavyAttack"} local TARGET_PART = "UpperTorso" local combatEvent = ReplicatedStorage:WaitForChild("Events"):WaitForChild("CombatEvent") local enemies = {} local function isEnemy(obj) return obj:IsA("Model") and obj:FindFirstChild("Humanoid") and not Players:GetPlayerFromCharacter(obj) end local function addEnemy(obj) if isEnemy(obj) then table.insert(enemies, obj) local posStr = "unknown" local root = obj:FindFirstChild("HumanoidRootPart") or obj.PrimaryPart if root then posStr = tostring(root.Position) end print("Enemy added:", obj.Name, "Position:", posStr) end end local function removeEnemy(obj) for i, e in ipairs(enemies) do if e == obj then table.remove(enemies, i) print("Enemy removed:", obj.Name) break end end end for _, obj in ipairs(Workspace:GetDescendants()) do addEnemy(obj) end Workspace.ChildAdded:Connect(addEnemy) Workspace.ChildRemoved:Connect(removeEnemy) spawn(function() while wait(1) do local new = {} for _, obj in ipairs(Workspace:GetDescendants()) do if isEnemy(obj) then table.insert(new, obj) end end enemies = new print("Enemy list refreshed, count:", #enemies) end end) local lastAttack = 0 RunService.Heartbeat:Connect(function() local now = tick() if now - lastAttack < COOLDOWN then return end local myPos = myRoot.Position local attacked = false for _, npc in ipairs(enemies) do local targetPart = npc:FindFirstChild(TARGET_PART, true) or npc:FindFirstChild("HumanoidRootPart") or npc.PrimaryPart if targetPart then local dist = (myPos - targetPart.Position).Magnitude if dist <= ATTACK_RANGE then for _, at in ipairs(ATTACK_TYPES) do combatEvent:FireServer(at, targetPart) end attacked = true print("Attacked:", npc.Name, "Distance:", dist, "Types:", table.concat(ATTACK_TYPES, ",")) end end end if attacked then lastAttack = now end end)