Zombie script not working well or they're laggy

  1. What do you want to achieve? Keep it simple and clear!

I have zombies for my game that I hope reaches front page. I have 1 main script that controls all the zombies(2 zombies) and I want them to move smoothly

  1. What is the issue? Include screenshots / videos if possible!

The issue is that sometimes the zombies teleport when they get close. Ik its pretty confusing but if they’re like 2 studs away from u they just suddenly become next to u.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have tried to reread the code but the only solution that’s reasonable for me is just lag. I have tried to do it in hidden developers but this dude just said I wasn’t putting in effort and that I should reread my code and that the code does what I tell it to do which is true but it wasn’t helpful at all. I honestly wouldn’t be surprised if it’s lag because I just could not see a single issue.

local tagged = game:GetService("CollectionService")
local distance = 50

local FindNearestChar = function(hrp)
    local target = 50
    local kill = nil
    for i, v in pairs(game.Players:GetPlayers()) do
        if v.Character then
            if v.Character:FindFirstChild("HumanoidRootPart") then
                if (v.Character.HumanoidRootPart.CFrame.Position - hrp.CFrame.Position).Magnitude <= distance and (v.Character.HumanoidRootPart.CFrame.Position - hrp.CFrame.Position).Magnitude <= target then
                    target = (v.Character.HumanoidRootPart.CFrame.Position - hrp.CFrame.Position).Magnitude
                    kill = v.Character
                end
            end
        end
    end
    return kill
end

for i, npcs in pairs(tagged:GetTagged("Hostile")) do
    local klone = npcs:Clone()
    tagged:RemoveTag(klone, "Hostile")
    local walktrack = npcs.Humanoid:LoadAnimation(script.Walk)
    coroutine.wrap(function()
        while true do
            wait(.1)
            local char = FindNearestChar(npcs.HumanoidRootPart)
            if char and npcs.Humanoid.Health > 0 then
                npcs.Humanoid:MoveTo(char.PrimaryPart.CFrame.Position)
                if not walktrack.IsPlaying then
                    walktrack:Play()
                end
            else
                npcs.Humanoid:MoveTo(npcs.HumanoidRootPart.Position)
                walktrack:Stop()
            end
        end
    end)()
    local function oof()
        npcs.Humanoid.Died:Connect(function()
            for lo, thing in pairs(npcs:GetChildren()) do    
                thing:Destroy()
            end
            for lol, v in pairs(klone:GetChildren()) do
                v.Parent = npcs
            end
            npcs.PrimaryPart = npcs:WaitForChild("HumanoidRootPart")
            klone:Destroy()
            klone = npcs:Clone()
            walktrack = npcs.Humanoid:LoadAnimation(script.Walk)
            oof()
        end)
    end
    oof()
end

no errors, occasionally the zombies stop and teleport and I’ll just blame it on lag

1 Like

gonna revive this instead of reposting but please i really want some good zombies and also can u tell me if there’s a more efficient method

1 Like

I had this problem many times when i made my own AIs and stuff, i had the same problem as you.
Check this article about optimization:

Sorry, i’m not good with explanations… but i have the solution for your issue. (i suppose you have basic knowledge about scripting)

local Zombie = script.Parent

function FindNearestPlayer()
	-- Your zombie AI probably has this function already
	-- Lets say this function returns the nearest player's torso
end

while Zombie.Humanoid.Health > 0 do
	local target_torso = FindNearestPlayer()
	if target_torso then
		local player = game:GetService("Players"):FindFirstChild(target_torso.Parent.Name)
		if player then
			 -- Sets the target as the network owner (if the target's connection is bad the zombie will lag too)
			Zombie:SetNetworkOwner(player)
		else
			-- When you set it to nil, the server will be the owner instead of a player.
			Zombie:SetNetworkOwner(nil)
		end
		print(Zombie:GetNetworkOwner())
	end
	wait(0.2)
end

Contact me if my solution doesn’t work :wink:

1 Like

u can’t do setnetworkowner on a model

Sorry, it didn’t show me any errors when i tried to set the model’s NetworkOwner…
You just have to get every BasePart in your Zombie and set the NetworkOwner

local player = game:GetService("Players"):FindFirstChild(target_torso.Parent.Name)
if player then
	 -- Sets the target as the network owner (if the target's connection is bad the zombie will lag too)
	for _,bodyPart in pairs(Zombie:GetDescendants()) do
		if bodyPart:IsA("BasePart") then
			Zombie:SetNetworkOwner(player)
		end
	end
else
	-- When you set it to nil, the server will be the owner instead of a player.
	for _,bodyPart in pairs(Zombie:GetDescendants()) do
		if bodyPart:IsA("BasePart") then
			Zombie:SetNetworkOwner(nil)
		end
	end
end

It is this most likely, I recommend reducing the wait time to just wait()

This can open a whole new can of issues with zombies teleporting due to network owners (Potentially an exploiter) teleporting the zombies away.

2 Likes

Set the NetworkOwnerShip on zombie’s primary part.