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
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:
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.