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)
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).UnitConfigurations.Speed.Value
bodyGyro.CFrame = mapPath[tostring(target)].CFrame
end
end
else
Directory:Destroy()
end
end)