Mouse.hit.p issue

So when I fire my gun at the skybox it doesn’t get a position, so what do I use to get a position?

local FireDb = false

script.Parent.Fire.OnServerEvent:Connect(function(Player, MousePos)
	if FireDb == false then
		if Player.Character.Humanoid.Health > 0 then
			if script.Parent.Stats.Ammo.Value > 0 then
				FireDb = true
				script.Parent.Stats.Ammo.Value = script.Parent.Stats.Ammo.Value - 1
				local Handle = script.Parent.Handle
				local Spread = math.random()
	
			    local Ignore = Player.Character
				local RayCast = Ray.new(Handle.Position,(MousePos+Vector3.new(Spread,Spread,Spread) - Handle.Position).unit*300)
				local Part,Position,Normal = game.Workspace:FindPartOnRay(RayCast,Player.Character, false,true)
				
				local R = Ray.new(Part.Position, Vector3.new(0,-1*300,0)) -- First parameter is the origin and second is the direction. Since it is going downwards, the Y coordinate of the direction has to be negative
	
				local PartFound, Pos = workspace:FindPartOnRay(R)
	
				local Dist = (Pos - Handle.Position).magnitude
				if not Dist then Dist = 300 end
				local Lazer = Instance.new("Part")
				Lazer.Parent = game.Workspace
				Lazer.Anchored = true
				Lazer.CanCollide = false
				Lazer.Size = Vector3.new(0.2,0.2,Dist)
				Lazer.BrickColor = BrickColor.new("New Yeller")
				Lazer.Material = "Neon"
				Lazer.CFrame = CFrame.new(Handle.Position,Position)*CFrame.new(0,0,-Dist/2)
				game.Debris:AddItem(Lazer,0.05)
				
				if Part.Parent:FindFirstChild("Humanoid") then
					Part.Parent.Humanoid:TakeDamage(10)
				end
				wait(0.25)
				FireDb = false
			end
		end
	end
end)
1 Like

You have to check if the mouse is over anything. I’d recommend using Mouse.Target function and if it’s equal to ‘nil’ then the player is having a mouse on the air and that’s when laser shouldn’t do anything or you can script your own max destination of the laser and run it when the statement is true.