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
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
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.
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()
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)