How do I make it Still shoot when there is no Mouse.Hit.Position

I have a gun system but the ray doesnt create when i shoot in the sky, I believe it doesnt create it because there is no Mouse.Hit which I use for ray Positioning, so How to I avoid this path, I have a range but it doesnt work when I try shooting out of the guns range aswell. How do I also make it stop when it hits something other than the gun handle and player.

Current Script:

ShootEvent.OnServerEvent:Connect(function(player, GunTool, startPos, endPos, damage, Range)

	local direction = (endPos - GunTool.Handle.Position).Unit * Range
	local raycast = game.Workspace:Raycast(startPos, direction)

	print(raycast.Instance.Name, raycast.Instance.Parent.Name)
	if raycast then
		local model = raycast.Instance:FindFirstAncestorOfClass("Model")

		if model and model:FindFirstChild("Humanoid") and model:FindFirstChild("HumanoidRootPart") and model.Name ~= player.Character.Name then
			local humanoid = model.Humanoid

			if humanoid then
				if raycast.Instance.Name == "Head" then
					humanoid.Health -= (damage * 1.8)
					humanoid.Health -= damage
	CreateVisualSingle(startPos, endPos)

Mouse.Hit should always be defined. The wiki has this to say about the property:

The mouse’s internal ray extends for 1000 studs. If the mouse is not pointing at an object in 3D space (for example when pointing at the sky), this property will be 1000 studs away from the Workspace.CurrentCamera.

Maybe it’s the RaycastResult’s Hit that you’re trying to fetch here? This wouldn’t exist if you aimed at the skybox, but mouse.Hit would exist nonetheless.

1 Like

So if Im understanding correctly even if theres no mouse.Hit beforehand roblox makes it extend from the camera by 1,000 studs?