What I'm making wrong with this wandering script?

local function FollowPath(CurrentPosition, TargetPosition)
	local Path = PathfindingService:CreatePath(PathfindingParams)
	local success, errorMessage = pcall(function()
		Path:ComputeAsync(CurrentPosition, TargetPosition)
	end)
	
	if success and Path.Status == Enum.PathStatus.Success then
		-- Get the path waypoints
		local Waypoints = Path:GetWaypoints()
		local IsBlocked = false
		local WaypointIndex = 2
		local BlockedConnection, HumanoidMoving
		
		for i = 2, #Waypoints, 1 do
			local Marker = Instance.new("Part")
			Marker.Size = Vector3.new(0.25, 10, 0.25)
			Marker.Position = Vector3.new(0, 5, 0) + Waypoints[i].Position
			Marker.Color = Color3.fromRGB(0, 255, 0)
			Marker.Anchored = true
			Marker.CanCollide = false
			Marker.Parent = workspace
		end
		
		HumanoidMoving = Body.Humanoid.MoveToFinished:Connect(function()
			WaypointIndex = WaypointIndex + 1
			print(WaypointIndex)
			print(#Waypoints)
			if #Waypoints <= WaypointIndex then
				Body.Humanoid:MoveTo(Waypoints[WaypointIndex].Position)
				print("NPC moved!")
			else
				BlockedConnection:Disconnect()
				BlockedConnection = nil
				HumanoidMoving:Disconnect()
				HumanoidMoving = nil
				print("NPC finished!")
			end
		end)	
		BlockedConnection = Path.Blocked:Connect(function(BlockedWaypointIndex)
			-- Check if the obstacle is further down the path
			if BlockedWaypointIndex >= WaypointIndex + 1 then
				IsBlocked = true
				-- Stop detecting path blockage until path is re-computed
				BlockedConnection:Disconnect()
				-- Call function to re-compute new path
				FollowPath(Waypoints[WaypointIndex].Position, TargetPosition)
			end
		end)
		Body.Humanoid:MoveTo(Waypoints[WaypointIndex].Position)
		--[[for WaypointIndex = 2, #Waypoints, 1 do
			if Waypoints[WaypointIndex].Action == Enum.PathWaypointAction.Walk then
				local EstimatedTime = (Waypoints[WaypointIndex - 1].Position - Waypoints[WaypointIndex].Position).Magnitude / ConvertToStuds(Parametres.Walkspeed)
				local Time = 0
				local Orientation = CFrame.new(Vector3.new(Waypoints[WaypointIndex - 1].Position.X, 0, Waypoints[WaypointIndex - 1].Position.Z), Vector3.new(Waypoints[WaypointIndex].Position.X, 0, Vector3.new(Waypoints[WaypointIndex].Position.Z))).Rotation
				while Time < EstimatedTime do
					local Delta = RunService.Heartbeat:Wait()
					Time = math.min(Time + Delta, EstimatedTime)
					Body:SetPrimaryPartCFrame(CFrame.new(Waypoints[WaypointIndex - 1].Position + Vector3.new(0, 3, 0)):Lerp(CFrame.new(Waypoints[WaypointIndex].Position + Vector3.new(0, 3, 0)), Time / EstimatedTime) * Orientation)
				end
			end
		end]]
	end
end

local function RandomWander()
	local CurrentPosition = Body.HumanoidRootPart.Position
	--[[local CurrentMagnitude = (Vector3.new(Parametres.WeldingPoint.X, 0, Parametres.WeldingPoint.Z) - Vector3.new(CurrentPosition.X, 0, CurrentPosition.Z))
	local CurrentProportion = (CurrentMagnitude / ConvertToStuds(Parametres.MaxWanderDistance))
	local CurrentX = (Parametres.WeldingPoint.X - CurrentPosition.X) / CurrentProportion
	local CurrentZ = (Parametres.WeldingPoint.Z - CurrentPosition.Z) / CurrentProportion
	
	local MaxWanderX = ]]
	
	local Abandon = 5
	local success = false
	local TargetPosition
	repeat
		Abandon = Abandon - 1
		local WanderX = math.random(-ConvertToStuds(Parametres.SingleWanderDistance), ConvertToStuds(Parametres.SingleWanderDistance))
		local WanderZ = if math.random(0, 1) == 0 then -1 else 1 * math.asin(math.cos(WanderX / ConvertToStuds(Parametres.SingleWanderDistance))) * ConvertToStuds(Parametres.SingleWanderDistance)
		TargetPosition = CurrentPosition + Vector3.new(WanderX, 0, WanderZ)
		if (Vector3.new(Parametres.WeldingPoint.X, 0, Parametres.WeldingPoint.Z) - Vector3.new(TargetPosition.X, 0, TargetPosition.Z)).Magnitude > ConvertToStuds(Parametres.MaxWanderDistance) then
			TargetPosition = (Vector3.new(Parametres.WeldingPoint.X, 0, Parametres.WeldingPoint.Z) - Vector3.new(TargetPosition.X, 0, TargetPosition.Z)).Unit * ConvertToStuds(Parametres.SingleWanderDistance) + Vector3.new(Parametres.WeldingPoint.X, 0, Parametres.WeldingPoint.Z)
		end
		TargetPosition = workspace:Raycast(TargetPosition + Vector3.new(0, 50, 0), Vector3.new(0, -100, 0), TargetPositionRaycastParams)
		if TargetPosition then
			if not TargetPosition.Instance:IsDescendantOf(workspace.VolgaMakara) then
				TargetPosition = TargetPosition.Position
				success = true
			end
		end
	until success or Abandon == 0
	if success == true then
		local Marker = Instance.new("Part")
		Marker.Size = Vector3.new(0.5, 10, 0.5)
		Marker.Position = Vector3.new(0, 5, 0) + TargetPosition
		Marker.Color = Color3.fromRGB(255, 0, 0)
		Marker.Anchored = true
		Marker.CanCollide = false
		Marker.Parent = workspace
		FollowPath(CurrentPosition, TargetPosition)
	else
		warn("Destination was out of bounds")
	end
end

while true do
	RandomWander()
	wait(10)
end

Hello, I’m making NPC, which will wander near part. But my NPC idk why just getting stuck each time. Can someone help me, and say, what’s wrong with it??

1 Like