Well, that I do not go for the loop with GetPlayers () I give debug and jump to the end and stop. I've tried a simple for loop and it works.
local PathfindingService = game:GetService("PathfindingService") local Players = game:GetService("Players"):GetPlayers() local enemy = script.Parent local humanoidEnemy = enemy.Humanoid local humanoidRootPart = enemy.HumanoidRootPart local Start = script.Parent.Parent.Start local Finish = script.Parent.Parent.Finish local currentDestination = Start for i , player in pairs(Players) do local character = game.Workspace:WaitForChild(player.Name) print(player.Name) if character then while wait(1) do local mag = (character.Head.Position - enemy.Head.Position).magnitude if mag <= 50 then print("Attack") else print(mag) while wait(2) do local startingPosition = humanoidRootPart.Position local goalPosition = currentDestination.Position local path = PathfindingService:FindPathAsync(startingPosition, goalPosition) local waypoints = path:GetWaypoints() for waypointIndex, waypoint in pairs(waypoints) do local waypointPosition = waypoint.Position humanoidEnemy:MoveTo(waypointPosition) humanoidEnemy.MoveToFinished:Wait() end if currentDestination == Start then currentDestination = Finish else currentDestination = Start end end end end end end
You're getting the players right when the server starts, which more often than not, the game doesn't see any players, yet. You'd have to give it a chance to at least get the 1st player. Also an FYI, this script will only be getting the first player that joins. To actively keep getting all players, you can use a PlayerAdded event to have this for each player everytime they join, or put the for loop in a while loop
local PathfindingService = game:GetService("PathfindingService") local enemy = script.Parent local humanoidEnemy = enemy.Humanoid local humanoidRootPart = enemy.HumanoidRootPart local Start = script.Parent.Parent.Start local Finish = script.Parent.Parent.Finish local currentDestination = Start wait(2) local Players = game:GetService("Players"):GetPlayers() for i , player in pairs(Players) do local character = game.Workspace:WaitForChild(player.Name) print(player.Name) if character then while wait(1) do local mag = (character.Head.Position - enemy.Head.Position).magnitude if mag <= 50 then print("Attack") else print(mag) while wait(2) do local startingPosition = humanoidRootPart.Position local goalPosition = currentDestination.Position local path = PathfindingService:FindPathAsync(startingPosition, goalPosition) local waypoints = path:GetWaypoints() for waypointIndex, waypoint in pairs(waypoints) do local waypointPosition = waypoint.Position humanoidEnemy:MoveTo(waypointPosition) humanoidEnemy.MoveToFinished:Wait() end if currentDestination == Start then currentDestination = Finish else currentDestination = Start end end end end end end
Thank you, thank you very much, now I have the problem that it does not calculate the distance while advancing it stays in the distance from the beginning.
while true do local mag = (character.Head.Position - enemy.Head.Position).magnitude if mag <= 50 then print("Attack") else print(mag) while wait(2) do local startingPosition = humanoidRootPart.Position local goalPosition = currentDestination.Position local path = PathfindingService:FindPathAsync(startingPosition, goalPosition) local waypoints = path:GetWaypoints() for waypointIndex, waypoint in pairs(waypoints) do local waypointPosition = waypoint.Position humanoidEnemy:MoveTo(waypointPosition) humanoidEnemy.MoveToFinished:Wait() end if currentDestination == Start then currentDestination = Finish else currentDestination = Start end end end wait() end