I made an NPC that follows you to kill you, but when you kill it, you respawn, and ten seconds later he respawns, but he stops following you. I think it has to do with the game.Players.PlayerAdded function but that is the only way I know. The script is inside the NPC
game.Players.PlayerAdded:Connect(function(p)
p.CharacterAdded:Connect(function(char)
while true do
wait(0.1)
if p:DistanceFromCharacter(script.Parent.HumanoidRootPart.Position) < 90 then
script.Parent.Humanoid:MoveTo(char.HumanoidRootPart.Position)
end
end
end)
end)

10 comments
When a player resets, their character disappears. So whenever the NPC tries to follow the character, the character is nil.
If that's the problem, then how can I make it so that the character is not nil when the player respawns
p:WaitForChild("Character")
I tried that and p.Character but they both don't work
You can instead check if the HumanoidRootPart is nil: if p.Character:FindFirstChild("HumanoidRootPart") then
Okay so now it detects the HumanoidRootPart but he won't move
Do this: script.Parent.Humanoid:MoveTo(char.HumanoidRootPart.Position, char.HumanoidRootPart)
MoveTo() on the Humanoid has two parameters. The first is the point (Vector3) the Humanoid is trying to walk to, and the second is the part that's it's trying to walk to.
It doesn't seem to want to do anything
Oh, now I see. DistanceFromCharacter() is always calculating below 90 because the character being reset, which means that it's constantly calculating 0. And because the Character is nil when that condition is met, the NPC never moves. You have to check if the distance is above 0 before moving the NPC.