New function at ray's max range

I have a script that lets me teleport to the mouse position, raycasted from the character and mouse with a range of 80.
But, once the range is greater than 80, I get an error because the ray doesn’t exist that far.
How can I instead make it do something at the max range?

local Origin = player.Character.Head.Position
local Direction = Mouse.Hit.LookVector*80
local Rayparams = RaycastParams.new()
Rayparams.FilterDescendantsInstances = {player.Character}
Rayparams.FilterType = Enum.RaycastFilterType.Blacklist
local ray = Ray.new(Origin, Direction, Rayparams)
local Result = game.Workspace:Raycast(ray.Origin, ray.Direction, Rayparams)
if Result then
	local raypos = CFrame.new(Result.Position)
	HRP.CFrame = raypos

I ‘solved’ this by doing

elseif not Result then
				local Origin = CFrame.new(ray.Origin)
				HRP.CFrame = (Origin + ray.Direction)
			end

If anyone still has a solution to the original question then I would still like an answer!

That is the solution—you don’t have a raycast result so you just use the tip of the ray instead.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.