So I want to every 1 second spawn a new NPC and make it move to x location. Using the moveto function doesn’t seem to work properly, after a few moments the npc stops moving towards the destination. So I need a loop to constantly make him walk the distance. How can I run a loop to spawn an npc, at the same time also loop that npc to walk towards the target position?
I need to do this via local script as well, needs to be done client side in my case.
My current script, only runs once in the main loop.
task.wait(20)
print("go")
walkAnim = "3489174223"
function playNewAnimation(track, whichHumanoid, looped)
for i,v in pairs(whichHumanoid:GetPlayingAnimationTracks()) do
v:Stop()
end
local Animator = whichHumanoid:WaitForChild("Animator")
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://" .. track
local shockAnimationTrack = Animator:LoadAnimation(animation)
shockAnimationTrack.Priority = Enum.AnimationPriority.Action
shockAnimationTrack.Looped = looped
shockAnimationTrack:Play()
end
function spawnNewZombie()
local newZombie = game:GetService("Lighting"):WaitForChild("TurretMissionZombie"):Clone()
newZombie.Parent = workspace
local countSpawns = workspace:WaitForChild("TurretZombieSpawns_1"):GetChildren()
local whichLocation = countSpawns[math.random(1,#countSpawns)]
newZombie:MoveTo(whichLocation.Position)
playNewAnimation(walkAnim, newZombie.Zombie, true)
while true do
newZombie.Zombie:MoveTo(workspace:WaitForChild("DummyTarget").Position)
task.wait(1)
end
end
while true do
task.wait(1)
spawnNewZombie() -- this loop gets stuck once this function runs
end
Don’t wanna waste resources, I plan to make a lot of NPC’s. Unless I keep computing their path (which would be expensive) then they will just run into themselves once one walks in front of the other.
task.wait(20)
print("go")
walkAnim = "3489174223"
function playNewAnimation(track, whichHumanoid, looped)
for i,v in pairs(whichHumanoid:GetPlayingAnimationTracks()) do
v:Stop()
end
local Animator = whichHumanoid:WaitForChild("Animator")
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://" .. track
local shockAnimationTrack = Animator:LoadAnimation(animation)
shockAnimationTrack.Priority = Enum.AnimationPriority.Action
shockAnimationTrack.Looped = looped
shockAnimationTrack:Play()
end
local zombieTable = {}
function spawnNewZombie()
local newZombie = game:GetService("Lighting"):WaitForChild("TurretMissionZombie"):Clone()
newZombie.Parent = workspace
local countSpawns = workspace:WaitForChild("TurretZombieSpawns_1"):GetChildren()
local whichLocation = countSpawns[math.random(1,#countSpawns)]
newZombie:MoveTo(whichLocation.Position)
playNewAnimation(walkAnim, newZombie.Zombie, true)
table.insert(zombieTable, newZombie)
end
function orderMovement()
for i,v in pairs(zombieTable) do
print(i,v)
v.Zombie:MoveTo(workspace:WaitForChild("DummyTarget").Position)
end
end
while true do
task.wait(1)
spawnNewZombie()
orderMovement()
end