local enemy = script.Parent
local waypoints = workspace.Waypoints
for waypoint = 1, #waypoints:GetChildren() - 2 do
repeat enemy.Humanoid:MoveTo(waypoints["Waypoint" .. tostring(waypoint)].Position); until enemy.Humanoid.MoveToFinished:Wait()
end
enemy.Humanoid:MoveTo(waypoints.Endpoint.Position)
enemy.Humanoid.Touched:Connect(function(touchingPart)
if touchingPart.Name == "Endpoint" then
enemy:Destroy()
end
end)
enemy.Humanoid.Died:Connect(function()
enemy:Destroy()
end)
the top part of the code is just me moving the npc, as this is a tower defense game, but the last 3 lines are what im trying to use to destroy the npc upon death.
This is because the event is being created after the enemy has moved through all the waypoints, you can fix it by moving them in front like this:
local enemy = script.Parent
local waypoints = workspace.Waypoints
enemy.Humanoid.Touched:Connect(function(touchingPart)
if touchingPart.Name == "Endpoint" then
enemy:Destroy()
end
end)
enemy.Humanoid.Died:Connect(function()
enemy:Destroy()
end)
for waypoint = 1, #waypoints:GetChildren() - 2 do
repeat enemy.Humanoid:MoveTo(waypoints["Waypoint" .. tostring(waypoint)].Position); until enemy.Humanoid.MoveToFinished:Wait()
end
enemy.Humanoid:MoveTo(waypoints.Endpoint.Position)