How to limit distance of the light

So I made this simple script that projects a light on the player’s cursor but I want there to be a dist limit for it

local LightSource = script.LightSource

local UIS = game:GetService("UserInputService")

local Key = Enum.KeyCode.F

local Toggled = false

local RunService = game:GetService("RunService")

local Mouse = script.Parent.Parent:GetMouse()

RunService.RenderStepped:Connect(function()
	
	if Toggled == true then
		
		if workspace:FindFirstChild("LightSource") then
			
			Mouse.TargetFilter = workspace:FindFirstChild("LightSource")
			
			workspace:FindFirstChild("LightSource").CFrame = CFrame.new(Mouse.Hit.Position + Vector3.new(0,0.5,0))
			
		end
		
	end
	
end)

UIS.InputBegan:Connect(function(input,gameProcessedEvent)
	
	if input.KeyCode == Key and not gameProcessedEvent then
		
		Toggled = not Toggled
		
		if Toggled == true then
			
			LightSource:Clone().Parent = workspace
			
		else
			
			if workspace:FindFirstChild("LightSource") then workspace:FindFirstChild("LightSource"):Destroy() end
			
		end
		
	end
	
end)

I don’t know what LightSource is, but I’m going to assume that it’s one of PointLight, SpotLight, or SurfaceLight. Those three classes have a Range parameter where you can set the range of the light. The max is 60 I think.

I think that you mean max distance away from the mouse. In that case, you should look into Player:DistanceFromCharacter(). This function returns the distance between a Vector3 and the player’s head.

You can use an if statement to check if it’s far away like so:

if player:DistanceFromCharacter(mouse.Hit.Position) <= 60 then
-- do stuff
end

60 being the max limit