Pathfinding: waypoint position cannot be assigned to

Currently I’m raycasting from a position above the waypoint downwards in order to detect the height of the smooth terrain underneath the waypoint.
Everything works, except I can’t change the position of the waypoint to the position of where the raycast ended?

Module script

local function RayCast_Downwards_To_Check_Terrain_Height(Waypoint, WaypointBall)
	local RayOrigin = Waypoint.Position + Vector3.new(0,100,0)
	local RayDirection = Vector3.new(0, -150, 0)

	-- Build raycast paramters
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character:GetChildren(), WaypointBall}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	-- Cast Ray
	local raycastResult = workspace:Raycast(RayOrigin, RayDirection, raycastParams)
	if raycastResult then
		if raycastResult.Instance.Name == "Terrain" then
			return(raycastResult.Position)	-- End position of raycast
		end
	end
end

local Waypoints = Path:GetWaypoints()
for _, Waypoint in pairs(Waypoints) do
	local RayEndPos = RayCast_Downwards_To_Check_Terrain_Height(Waypoint)	-- Call the raycast function
	Waypoint.Position = RayEndPos  -- Change waypoint position to raycast end point position

	local part = Instance.new("Part")
	part.Shape = "Ball"
	part.Material = "Neon"
	part.Size = Vector3.new(0.6, 0.6, 0.6)
	part.Position = Waypoint.Position
	part.Anchored = true
	part.CanCollide = false
	part.Parent = game.Workspace
end

PathWaypoint is a datatype. It isn’t a property. (sorta)

local waypoint = PathWaypoint.new(pos, Enum.PathWaypointAction.Walk) -- this is how you should create a new waypoint instead

There is an article you can look at PathWaypoint | Roblox Creator Documentation
Hope this helps :slight_smile:

Also do you really need the waypoints to be on the ground?

Absolute life saver thanks.

The waypoints are positioned at the same position of the raycast’s end point since pathfinding isn’t very good on smooth terrain, going through, instead of over hills and such.

1 Like