Why is Mouse.Hit nil?

I’m trying to make a projectile, but the issue is that I keep getting this error that says

Players.StarJ3M.Backpack.RocketLauncher.Server:15: attempt to index nil with 'Hit'

I’m not sure why I keep getting this error, but here is the local and server script.

local tool = script.Parent
local RemoteEvent = tool:WaitForChild("RemoteEvent")

local cooldownTime = 2.5
local cooldown = false

local mouse = game.Players.LocalPlayer:GetMouse()

tool.Activated:Connect(function()
	if not cooldown then
		cooldown = true
		RemoteEvent:FireServer(mouse)
		print(mouse)
		task.wait(cooldownTime)
		cooldown = false
	end
end)
local tool = script.Parent
local RemoteEvent = tool:WaitForChild("RemoteEvent")

RemoteEvent.OnServerEvent:Connect(function(player, mouse)
	local Rocket = Instance.new("Part") do
		-- Set up the Rocket
		Rocket.Name = "Rocket"
		Rocket.Size = Vector3.new(4, 1, 1)
		Rocket.CanCollide = false
		Rocket.Parent = workspace
	end
	
	print(mouse)
	
	Rocket.CFrame = CFrame.new(tool.Handle.Position, mouse.Hit.Position)
	
	local BodyVelocity = Instance.new("BodyVelocity") do
		-- Set up the BodyVelocity
		BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
		BodyVelocity.Velocity = (mouse.Hit.Position - tool.Handle.Position).Unit * 100
		BodyVelocity.Parent = Rocket
	end
	
	task.wait(10)
	
	Rocket:Destroy()
end)

This is happening because the Mouse object is only supposed to be used by the client, so it’s nil on the server.

2 Likes

Try checking if its not nil.

if mouse then --if mouse exists then
   --Code
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.