I want to make my pathfinding more smooth
I’ve searched for around 2 days and couldn’t find anything that works with my script
local path = PathfindingService:CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = false,
AgentCanClimb = false;
Costs = {
Water = 20;
Snow = math.huge;
Basalt = math.huge;
}
})
local character = clone
local humanoid = character:WaitForChild("Humanoid")
local waypoints --= {workspace.TestF.Checker2, workspace.TestF.Checkpoint2, workspace.TestF.End2}
local nextWaypointIndex
local reachedConnection
local blockedConnection
local function followPath(destination, Goinside)
-- Compute the path
local success, errorMessage = pcall(function()
path:ComputeAsync(character.PrimaryPart.Position, destination)
end)
if success and path.Status == Enum.PathStatus.Success then
-- Get the path waypoints
waypoints = path:GetWaypoints()
-- Detect if path becomes blocked
blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
-- Check if the obstacle is further down the path
if blockedWaypointIndex >= nextWaypointIndex then
-- Stop detecting path blockage until path is re-computed
blockedConnection:Disconnect()
-- Call function to re-compute new path
followPath(destination)
end
end)
-- Detect when movement to next waypoint is complete
if not reachedConnection then
reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
if reached and nextWaypointIndex < #waypoints then
-- Increase waypoint index and move to next waypoint
nextWaypointIndex += 1
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
if Chosen == 2 then
path = PathfindingService:CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = false,
AgentCanClimb = false;
Costs = {
Water = 20;
Snow = math.huge;
Basalt = 1
}
})
workspace.Values.Bank.Value += 1
elseif Chosen == 1 then
clone:Destroy()
local PlayersFriends2 = {}
local plr
for i, player in pairs(game.Players:GetChildren()) do
plr = player
end
local success, page = pcall(function() return players:GetFriendsAsync(plr.UserId) end)
if success then
repeat
local info = page:GetCurrentPage()
for i, friendInfo in pairs(info) do
table.insert(PlayersFriends2, friendInfo.Id)
end
if not page.IsFinished then
page:AdvanceToNextPageAsync()
end
until page.IsFinished
end
Npc.SpawmMultipleNpcs(PlayersFriends2, 1)
end
reachedConnection:Disconnect()
blockedConnection:Disconnect()
end
end)
end
-- Initially move to second waypoint (first waypoint is path start; skip it)
nextWaypointIndex = 2
humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
else
warn("Path not computed!", errorMessage, clone)
wait(.1)
followPath(destination)
end
end
local random = math.random(10, workspace.Values.ChanceOfSpawning.Value)
if workspace.Values.Open.Value == true and random >= 11 then
Chosen = 2
followPath(workspace.TestF.inside1.Position)
else
if FinalSpawn == Spwn2 then
Chosen = 1
followPath(workspace.TestF.End2.Position)
else
followPath(workspace.TestF.End.Position)
end
end