local Players = game:GetService("Players") local RunService = game:GetService("RunService") local ReplicatedStorage = game:GetService("ReplicatedStorage") local LocalPlayer = Players.LocalPlayer local ZombiesFolder = workspace:WaitForChild("Zombies_Local") local GunRemote = ReplicatedStorage.GunRemotes:WaitForChild("GunHit") local MAX_TARGETS = 8 local FIRE_DELAY = 0.03 local lastFire = 0 local CurrentWeapon = nil local function UpdateWeapon() local character = LocalPlayer.Character if not character then CurrentWeapon = nil return end CurrentWeapon = nil for _, v in ipairs(character:GetChildren()) do if v:IsA("Tool") then CurrentWeapon = v.Name break end end end local function SetupCharacter(character) UpdateWeapon() character.ChildAdded:Connect(function(child) if child:IsA("Tool") then CurrentWeapon = child.Name end end) character.ChildRemoved:Connect(function(child) if child:IsA("Tool") and CurrentWeapon == child.Name then task.wait() UpdateWeapon() end end) end if LocalPlayer.Character then SetupCharacter(LocalPlayer.Character) end LocalPlayer.CharacterAdded:Connect(SetupCharacter) local function GetClosestZombies() local character = LocalPlayer.Character if not character then return {} end local hrp = character:FindFirstChild("HumanoidRootPart") if not hrp then return {} end local zombieList = {} for _, zombie in ipairs(ZombiesFolder:GetChildren()) do if zombie:IsA("Model") then local pos = zombie:GetPivot().Position local distance = (hrp.Position - pos).Magnitude table.insert(zombieList, { Model = zombie, Distance = distance, Position = pos }) end end table.sort(zombieList, function(a, b) return a.Distance < b.Distance end) return zombieList end RunService.Heartbeat:Connect(function() if tick() - lastFire < FIRE_DELAY then return end lastFire = tick() if not CurrentWeapon then UpdateWeapon() end if not CurrentWeapon then return end local targets = GetClosestZombies() local sent = 0 for _, data in ipairs(targets) do if sent >= MAX_TARGETS then break end local zombie = data.Model local zombieId = tonumber(zombie.Name:match("%d+")) if zombieId then GunRemote:FireServer( CurrentWeapon, zombieId, data.Position ) sent += 1 end end end)