Any solution for this lag?

So uh, i was trying to make a TD game (like TDS or TB) for me and my friends, but i have a problem with zombies… Sometimes they just freeze for half second, probably due to replicating lag (this problem doesnt apply when you press “Run” instead of playing the game)

Gif

https://gyazo.com/23acbc5ac48410137d20f09ba2cb2580

Code

local Directory = script.Parent
local Model = Directory.Zombie
local Configurations = Directory:WaitForChild(“Config”)
local RStorage = game:GetService(“ReplicatedStorage”)

local currentMap = script.Parent.Parent.Parent
local mapPath = currentMap:WaitForChild(“Nodes”)

local bodyVel = Model.HumanoidRootPart.BodyVelocity
local bodyGyro = Model.HumanoidRootPart.BodyGyro

local enemySpawn = game.Workspace.Map.enemySpawn
Model.HumanoidRootPart.CFrame = enemySpawn.CFrame

local target = 1
–bodyVel.Velocity = (mapPath[tostring(target)].Position-Model.HumanoidRootPart.Position).Unit*enemyConfig.Speed.Value
–bodyGyro.CFrame = mapPath[tostring(target)].CFrame

Model.HumanoidRootPart.Touched:Connect(function(hit)
if target <= #mapPath:GetChildren() and Configurations.Health.Value > 0 then
if hit.Parent == mapPath or hit.name == “enemySpawn” then
bodyVel.Velocity = (mapPath[tostring(target)].Position-Model.HumanoidRootPart.Position).UnitConfigurations.Speed.Value
bodyGyro.CFrame = mapPath[tostring(target)].CFrame
local rootDist = (mapPath[tostring(target)].Position-Model.HumanoidRootPart.Position).magnitude
if rootDist < Configurations.TurnDist.Value and target <= #mapPath:GetChildren() and Configurations.Health.Value > 0 then
target = target + 1
bodyVel.Velocity = (mapPath[tostring(target)].Position-Model.HumanoidRootPart.Position).Unit
Configurations.Speed.Value
bodyGyro.CFrame = mapPath[tostring(target)].CFrame
end
end
else
Directory:Destroy()
end
end)

1 Like

so basically you can disables the NPCS climbing and things they dont use like if your NPC dosent climb disable climbing

1 Like

Nothing changed at all, and zombies can’t collide with other zombies so i don’t think they could climb

Maybe instead of putting a script inside each zombie, you could use a Module script in server script service with coroutines so it’s Asynchronous.

3 Likes

The stutter is probably from Network ownership changing when it reaches the switch-off radius. Just set the zombies network ownership to server only.

cool_zombie.HumanoidRootPart:SetNetworkOwner(nil)
4 Likes

Thanks, it worked! I never knew about this, i’m gonna search it on the dev hub, thanks again|

Make sure you also consider that Roblox servers tend to have high ping, especially when dealing with large amounts of parts. you script looked pretty clean though.

As a builder I would recommend grouping up as many parts as you can to reduce memory usage. This way players affected by this issue should have an easier time playing smoothly. Good luck!