Why does my npc get destroyed before even getting close to the end?

for some reason, my npc destroys before It gets anywhere. Can you help me figure this out?

Im trying to make a moving npc for my tower defense game but its not working out for me.

this is my code

spawn(function()
	local zombie = game.ReplicatedStorage.Zombie:Clone()
	zombie.Humanoid.WalkSpeed = 0.5
	zombie.Parent = game.Workspace
	local humanoid = zombie.Humanoid
	zombie.PrimaryPart.CFrame = CFrame.new(game.Workspace.Spawn.Position)
	
	-- Variables for the point(s) the zombie should move between
	local pointA = game.Workspace.point1
	local pointb = game.Workspace.point2
	local pointC = game.Workspace.point3
	local pointD = game.Workspace.point4
	local pointE = game.Workspace.point5
	
	
	
	-- Move the zombie to the primary part of the green flag model
	humanoid:MoveTo(pointA.Position)
	humanoid.MoveToFinished:Wait()
	humanoid:MoveTo(pointb.Position)
	humanoid.MoveToFinished:Wait()
	humanoid:MoveTo(pointC.Position)
	humanoid.MoveToFinished:Wait()
	humanoid:MoveTo(pointD.Position)
	humanoid.MoveToFinished:Wait()
	humanoid:MoveTo(pointE.Position)
	humanoid.MoveToFinished:Wait()
		zombie:Destroy()
		
		
		
end)

and for some reason the destroy script is running before the zombie even gets close and some of the moveto are also for some reason throwing the zombies of balance

2 Likes

Try print debugging, and then post the output here.

What do you mean try print debugging meaning where do you want me to print and the output has zero errors other than plugin errors which actually do nothing to gameplay or my scripts

image

I would suggest you use couroutine.create() Its more efficient.

Instead of :Wait() in

Use :Connect() and write the code that destroys the zombie in there. I think this might fix your script

So, you want me to use courountine.create when? Are you referring to my function type instead of using spawn? also, when my npc moves it won’t even wait to finish it just goes on to the next part and instead of going in a straight line it goes diagonally to the next part

I think its better to use coroutine.wrap()because its faster and more efficient than Spawn(). I sugessted to use coroutine.wrap()[Now, before i said create()] instead of Spawn(). If its not following the straight line, then use Robox Path Finding Service. It will help you move your zombie straight.

1 Like

Now my npc isn’t spawning at all. why do you think this is?

What is your new code by the way?

coroutine.create(function()
local zombie = game.ReplicatedStorage.Zombie:Clone()
zombie.Humanoid.WalkSpeed = 0.5
zombie.Parent = game.Workspace
local humanoid = zombie.Humanoid
zombie.PrimaryPart.CFrame = CFrame.new(game.Workspace.Spawn.Position)

-- Variables for the point(s) the zombie should move between
local pointA = game.Workspace.point1
local pointb = game.Workspace.point2
local pointC = game.Workspace.point3
local pointD = game.Workspace.point4
local pointE = game.Workspace.point5



-- Move the zombie to the primary part of the green flag model
humanoid:MoveTo(pointA.Position)
humanoid.MoveToFinished:Wait()
humanoid:MoveTo(pointb.Position)
humanoid.MoveToFinished:Wait()
humanoid:MoveTo(pointC.Position)
humanoid.MoveToFinished:Wait()
humanoid:MoveTo(pointD.Position)
humanoid.MoveToFinished:Wait()
humanoid:MoveTo(pointE.Position)
	humanoid.MoveToFinished:Wait()
	print("Debugging")
	zombie:Destroy()

end)

Cool, you created the coroutine thread. but you need to start the thread.

For example

local Thread = coroutine.create(function()
--All your code

end)

coroutine.resume(Thread)

Hope that helps!

Ok im gonna try right now. Thanks in advance!

ok so now my npcs are spawning but it still doesn’t wait all the way when I am moving the npc. why might this be?

It’s basically the issue of NetworkOwnerShip of NPC. It keeps switching between Client and Server.

Try setting the NetworkOwner to Server.
I have a piece of code here to do it:

function setNetworkOwner(character)
    for _, x in pairs(character:GetDescendants())do
        if x:IsA("BasePart") and x:CanSetNetworkOwnership() then
            x:SetNetworkOwner(nil)
        end
    end
end

 setNetworkOwner(Your Zombie NPC)

this doesn’t seem to work for some reason. have you tested this?