My script is lagging the whole server. I have found the reason why, it is due to a runservice connection being made for every zombie but I just can’t find an alternative. Pathfinding breaks my script as shown here:
PathFinding Attempt
local Waves = {}
local spawners_folder = game.Workspace.Spawners
local enemies = game:GetService("ServerStorage"):WaitForChild("Enemies")
local enemiesinworkspace = workspace.Enemies
local count = 0
function Waves.Spawn(zombie, amount, plr, char)
local Exists = enemies:FindFirstChild(zombie)
if Exists then
while true do
task.wait()
local clone = Exists:Clone()
for i,v in ipairs(clone:GetDescendants()) do
if v:IsA("BasePart") then
v.CollisionGroup = "Zombie"
end
end
count+=1
for i,v in pairs(spawners_folder:GetChildren()) do
clone.HumanoidRootPart.CFrame = v.CFrame
clone.Parent = enemiesinworkspace
end
local function CodeAi()
local zombieroot = clone.HumanoidRootPart
local hum = char.Humanoid
local zombiehumanoid = clone.Zombie
local humanoidroot = char.HumanoidRootPart
local renderservice = game:GetService("RunService")
local PathfindingService = game:GetService("PathfindingService")
local path = PathfindingService:CreatePath()
path:ComputeAsync(zombieroot.Position, humanoidroot.Position)
local waypoints = path:GetWaypoints()
for i,v in pairs(waypoints) do
zombiehumanoid:MoveTo(v.Position)
while true do
local distance = (zombieroot.Position - v.Position).Magnitude
local distance_from_player = (zombieroot.Position - humanoidroot.Position).Magnitude
if distance >100 then
zombiehumanoid:MoveTo(v.Position)
end
if distance_from_player >100 then
zombiehumanoid:MoveTo(v.Position)
end
task.wait()
end
end
end
if count == amount then
break
end
CodeAi()
end
else
warn("Requested Zombie " .. zombie .. " does not exist")
end
end
return Waves
Behaviour from pathfinding attempt:
How here is my script for the zombies that lags everything. It works but lags. It can either lag fps or the whole server through ping.
Script:
local Waves = {}
local spawners_folder = game.Workspace.Spawners
local enemies = game:GetService("ServerStorage"):WaitForChild("Enemies")
local enemiesinworkspace = workspace.Enemies
local count = 0
function Waves.Spawn(zombie, amount, plr, char)
local Exists = enemies:FindFirstChild(zombie)
if Exists then
while true do
task.wait(1)
local clone = Exists:Clone()
for i,v in ipairs(clone:GetDescendants()) do
if v:IsA("BasePart") then
v.CollisionGroup = "Zombie"
end
end
count+=1
for i,v in pairs(spawners_folder:GetChildren()) do
clone.HumanoidRootPart.CFrame = v.CFrame
clone.Parent = enemiesinworkspace
if count == amount then
break
end
end
function CodeAi()
local renderservice = game:GetService("RunService")
local hum = clone.Zombie
local humrootpart = char.HumanoidRootPart
local hitbox = clone.HitBox
local animator = hum.Animator
local animid_kill = clone.ZombieAttack
local animid_killgo = hum:LoadAnimation(animid_kill)
local distance = nil
local direction = nil
local zombieroot = clone.HumanoidRootPart
local debounce = false
local Conn = renderservice.Heartbeat:Connect(function()
local mag = (humrootpart.Position - zombieroot.Position)
distance = mag.Magnitude
direction = mag.Unit
hitbox.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if debounce == false then
debounce = true
hit.Parent.Humanoid:TakeDamage(5)
animid_killgo:Play()
task.wait(2)
debounce = false
end
end
end)
if distance <=100 then
hum:Move(direction)
elseif distance >=100 then
hum:Move(Vector3.new(math.random(1,50),math.random(1,2),math.random(1,50)))
end
end)
plr.Character.Humanoid.Died:Once(function()
Conn:Disconnect()
end)
end
coroutine.wrap(CodeAi)()
warn("Requested Zombie " .. zombie .. " does not exist")
end
end
end
return Waves
In client, it lags the server through ping(and when the player dies, the zombies continue spawning exceeded 16.)
In Roblox studio however, the fps drops significantly.
I can’t seem to find an alternative so this post is my last resort.