Coroutine, "killer NPC" loop w/ cloning

To sum up the code below I’m trying to make a “killer NPC” he walks between two points called ‘Start’ and ‘End’. On his way from End to start if you are within a distance of 15 or less it kills you. I’m fairly new to coroutines. v is a model called ‘Walking Person’, which holds start, end, and the NPC. It works until the character dies then he stops moving. Also this is server side so the item with ‘WalkingPerson’ in it is being cloned. And when there are 2 players or more in a server, it gets messed up, nothing moves. Hopefully, its just a way I wrote the coroutines. If you could give a hand that would be amazing, if not no worries!

Also, I know this isn’t complete code but the values being passed are true so I’m sure the mess up is coming from the coroutine.

local walkingPerson = coroutine.create(function(v, player)
    while true do
	    wait()
	    v:WaitForChild("NPC").Humanoid:MoveTo(v.End.Position)
	    v:WaitForChild("NPC").Humanoid.MoveToFinished:wait()
	    local targetReached = false
	    v:WaitForChild("NPC").Humanoid:MoveTo(v.Start.Position)
	    v:WaitForChild("NPC").Humanoid.MoveToFinished:Connect(function(successful)
	        targetReached = true
	    end)
	    while not targetReached do
		    local character = player.Character or player.CharacterAdded:wait()
	        if player:DistanceFromCharacter(v:WaitForChild("NPC").HumanoidRootPart.Position) <= 15 then
			    character:WaitForChild("UpperTorso"):Destroy()
	        end
	        wait()
	    end
    end
end)

if v.Name == "WalkingPerson" then
    coroutine.resume(walkingPerson, v, player)
end

You have an inner “while” loop:

while not targetReached do

The problem though is that targetReached is never flipped to true anywhere within that inner loop. Thus, you are stuck within it forever.

Does this not do it?

v:WaitForChild("NPC").Humanoid.MoveToFinished:Connect(function(successful)
    targetReached = true
end)
1 Like

The code here looks mostly fine from what I can tell. How is player defined? Is this script local or server side?

Server side. Player is being defined from the remote event.

I’ve recreated your code on a baseplate and it works fine with multiple players. Can you give the rest of the code or a repro place?

Can you send me how you set up your model? And your complete code to see how it compares? I basically am going through a model if the name is ‘Walking person’ then execute the coroutine. The model is set up like this:

  • Main model being cloned
    • Random models
    • Random models
    • WalkingPerson
      • Start
      • End
      • NPC

The first issue is that when you kill the player via removing the torso, the loop continues and yields for the torso of the same character model again, halting the continuation of the outer loop. Fix this by killing the player via setting Humanoid.Health to 0.

The second issue is that you are creating a single co-routine, and resuming it for each player. Fix this by creating the co-routine inside the PlayerAdded event function.

1 Like