Script helping mouse


local tool = script.Parent

	local Owner = tool.Parent.Name
	local player = game.Players:FindFirstChild(Owner)
	local mouse = player:GetMouse()
	local ray =, (mouse.Hit.p - tool.Barrel.CFrame.p).unit*300)
	local hit, position = game.Workspace:FindPartOnRay(ray, Owner)
	local humanoid = hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
	local distance = (position - tool.Barrel.CFrame.p).magnitude
	local rayPart ="Part", Owner)
	rayPart.Name          = "RayPart"
	rayPart.BrickColor    ="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          =, 0.2, distance)
	rayPart.CFrame        =, tool.Barrel.CFrame.p) *, 0, -distance/2)
	game.Debris:AddItem(rayPart, 0.02)

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?