Mouse Target/hit range

I’m making a teleport move, and I want there to be a max range on the teleport.

The script basically just checks if the mouse if hovering over a player and if it is it’ll teleport you to them

Problem is you’d be able to teleport anywhere and I’d prefer it to have a range of say 150 studs or so.

local target = mouse.Target
				--Add an if checking if target is within range (pls explain how it works)
				if target.Parent:FindFirstChild("Humanoid") then
					RCD = true
					script.Pika.R:FireServer(target)
					wait(6)
					RCD = false
				end	

Use a magnitude check to make sure that mouse.Hit.Position is within 150 studs

local HitPos = mouse.Hit.Position

if game:GetService("Players").LocalPlayer:DistanceFromCharacter(HitPos) <= 150 then
   if target.Parent:FindFirstChild("Humanoid") then
		RCD = true
		script.Pika.R:FireServer(target)
		task.wait(6)
		RCD = false
	end	
end
5 Likes

Tysm, I appreciate the help :pray: :handshake: