Hello all! I recently created a pathfinding script to let players move on a custom X Y coordinate grid. Everything works fine except the pathfinding is very choppy and laggy. Is there anything I’m doing wrong in this script? I have a feeling it has to do with the while loop, but I’m not quite sure, pathfinding and raycasting are two things that I’m not very good at.
local PathfindingService = game:GetService("PathfindingService")
local rooms = game.ReplicatedStorage.RoomPositions:GetChildren()
function pathFind(player)
local Humanoid = player.Character.Humanoid
local torso = player.Character.LowerTorso
local positionToMoveTo = game.Workspace.Game.Positions:FindFirstChild(player.TogetherPosition.Value) -- Finding the part in the workspace that corresponds to the position the player is at
local Path = PathfindingService:CreatePath() -- Creates a path
Path:ComputeAsync(torso.Position,positionToMoveTo.Position) -- Sets the start and end point
local wayPoints = Path:GetWaypoints() -- little points on the way to the destination
for index, waypoint in pairs(wayPoints) do
Humanoid:MoveTo(waypoint.Position)
Humanoid.MoveToFinished:Wait()
end
end
while wait() do
if game.ReplicatedStorage.RoundInProgress.Value == true then
local players = game.Players:GetPlayers()
for i,player in pairs(players) do
pathFind(player)
end
end
end
Also, ignore the Christmas decorations, hoping to launch the game this December.
Sorry the video itself is laggy, but it still shows the stopping and starting of my character.