Script helping mouse

Script:

local tool = script.Parent

tool.Activated:Connect(function()
	local Owner = tool.Parent.Name
	local player = game.Players:FindFirstChild(Owner)
	local mouse = player:GetMouse()
	local ray = Ray.new(tool.Barrel.CFrame.p, (mouse.Hit.p - tool.Barrel.CFrame.p).unit*300)
	local hit, position = game.Workspace:FindPartOnRay(ray, Owner)
	script.Parent.Handle.Fire:Play()
	local humanoid = hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		humanoid:TakeDamage(15)
	end
	local distance = (position - tool.Barrel.CFrame.p).magnitude
	local rayPart = Instance.new("Part", Owner)
	rayPart.Name          = "RayPart"
	rayPart.BrickColor    = BrickColor.new("White")
	rayPart.Material	  = "SmoothPlastic"
	rayPart.Transparency  = 0.5
	rayPart.Anchored      = true
	rayPart.CanCollide    = false
	rayPart.TopSurface    = Enum.SurfaceType.Smooth
	rayPart.BottomSurface = Enum.SurfaceType.Smooth
	rayPart.formFactor    = Enum.FormFactor.Custom
	rayPart.Size          = Vector3.new(0.2, 0.2, distance)
	rayPart.CFrame        = CFrame.new(position, tool.Barrel.CFrame.p) * CFrame.new(0, 0, -distance/2)
	game.Debris:AddItem(rayPart, 0.02)
end)

The Error: Players.Player1.Backpack.gun.Gun:7: attempt to index nil with ‘Hit’

If mouse is nil, then you’re retrieving the mouse from the server, you can only retrieve it from the Client so you’ll most likely need to change your code to use a RemoteEvent where the client passes in the position of where the mouse hit, and the server does the damaging and showing of the bullet

If i will use remote event code flow slows down. is there any other solution?