I am using pathfinding to make my zombies game, and I run into quite a lot of issues with it.
I represent here the path the zombie takes, and for some reason when there are above like 3 zombies it starts to lag and only run once per second, and my client memory is extremely high for some reason:
https://gyazo.com/64d03fcc2193b041f628661038577403.png
This is the non-smooth movement:
https://gyazo.com/aeaa59fded055f89f2920e626e205d2e
And this is the smooth movement I expect to have with all my zombies:
https://gyazo.com/7c128e1386fe41baa52178cd79cdaf41
My server runs one master script, that uses collection service to pretty much control all the zombies.
I use runService.Stepped to repeat the process, and this may not be efficient at all and i’d like some support and help on the topic.
This is my main code:
(My Zombies are called Zetos in this instance)
rs.Stepped:Connect(function()
for _,zeto in pairs(CollectionService:GetTagged("Zeto")) do -- Getting the Tagged Zombies
if zeto:FindFirstChild("Humanoid") then
if zeto.Humanoid.Health > 0 and zeto.Spawning.Value == false then -- checks if they are spawning
local human = zeto.Humanoid
local hroot = zeto1.HumanoidRootPart
local head = zeto1:FindFirstChild("Head")
local vars = zeto1.vars
human:SetStateEnabled(Enum.HumanoidStateType.Physics,false) -- i heard this helps?
local nrstt = GetTorso(hroot.Position,zeto) -- nrstt is a function that finds the player location
if nrstt ~= nil and human.Health > 0 then -- if player detected
vars.Chasing.Value = true
local function checkw(t) -- a function that pretty much calculates the current waypoint and returns when requested. (calculates everything in a NUTSHELL)
local ci = 3
if ci > #t then
ci = 3
end
if t[ci] == nil and ci < #t then
repeat ci = ci + 1 wait() until t[ci] ~= nil
return Vector3.new(1,0,0) + t[ci]
else
ci = 3
return t[ci]
end
end
path = pfs:FindPathAsync(hroot.Position, nrstt.Position)
waypoint = path:GetWaypoints()
local connection;
local direct = Vector3.FromNormalId(Enum.NormalId.Front)
local ncf = hroot.CFrame * CFrame.new(direct)
direct = ncf.p.unit
local rootr = Ray.new(hroot.Position, direct)
local ignorel = {workspace.Zetos.Storage,hroot,workspace.Rays}
local phit, ppos = game.Workspace:FindPartOnRayWithIgnoreList(rootr, ignorel)
if path and waypoint or checkw(waypoint) then
if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Walk then
human:MoveTo( checkw(waypoint).Position )
local part = Instance.new("Part") -- this is just to visualize the nodes
part.Shape = "Ball"
part.Material = "Neon"
part.Size = Vector3.new(0.6, 0.6, 0.6)
part.Position = checkw(waypoint).Position
part.Anchored = true
part.CanCollide = false
part.Parent = game.Workspace
game:GetService("Debris"):AddItem(part,.1)
human.Jump = false
end
if checkw(waypoint) ~= nil and checkw(waypoint).Action == Enum.PathWaypointAction.Jump then
connection = human.Changed:connect(function()
human.Jump = true
end)
human:MoveTo( waypoint[4].Position )
else
human.Jump = false
end
if connection then
connection:Disconnect()
end
else
for i = 3, #waypoint do
human:MoveTo( waypoint[i].Position )
end
end
path = nil
waypoint = nil
elseif nrstt == nil then -- if player not detected
vars.Chasing.Value = false
path = nil
waypoint = nil
human.MoveToFinished:Wait()
end
end
end
end
end)
Now, I need help realizing what’s wrong with the script, or my methods?