Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why am I getting the attempt to index global 'mouse' (a nil value) error?

Tool = script.Parent Player = Tool.Parent.Parent mouse = Player:GetMouse()

Tool.RemoteEvent.OnServerEvent:Connect(function()

local Ammo = 5

if Ammo > 0 then
	
	local Bullet = Instance.new("Part", game.Workspace)
	game.Debris:AddItem(Bullet, 2)
	Bullet.Shape = "Ball"
	Bullet.TopSurface = "Smooth"
	Bullet.BottomSurface = "Smooth"
	Bullet.BrickColor = BrickColor.new ("Dark stone grey")
    Bullet.CanCollide = false
    Bullet.CFrame = Tool.Handle.CFrame
     Bullet.CFrame = CFrame.new(Bullet.Position,mouse.Hit.p)

  local v = Instance.new("BodyVelocity")
   v.Parent = Bullet
  v.velocity = Bullet.CFrame.lookVector * 90

v.MaxForce = Vector3.new(math.huge, math.huge, math.huge) Ammo = Ammo - 1 print("You have" ..Ammo.. "ammo left in your gun!") end

end)

1 comment

0

Tool.Parent.Parent.mouse

1 answer

✓ Accepted answer

You cannot get the player's mouse from the server which is what I think you're doing since you're using OnServerEvent.

Try doing something like this

Tool = script.Parent 

Tool.RemoteEvent.OnServerEvent:Connect(function(Player, mouseHit) --//OnServerEvent first parameter is always the Player object that fired the event
local Ammo = 5
	if Ammo > 0 then
	    local Bullet = Instance.new("Part", game.Workspace)
	    game.Debris:AddItem(Bullet, 2)
	    Bullet.Shape = "Ball"
	    Bullet.TopSurface = "Smooth"
	    Bullet.BottomSurface = "Smooth"
	    Bullet.BrickColor = BrickColor.new ("Dark stone grey")
	    Bullet.CanCollide = false
	    Bullet.CFrame = Tool.Handle.CFrame
	    Bullet.CFrame = CFrame.new(Bullet.Position,mouseHit.p)
	
		local v = Instance.new("BodyVelocity")
		v.Parent = Bullet
		v.velocity = Bullet.CFrame.lookVector * 90
		v.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
		Ammo = Ammo - 1
		print("You have" ..Ammo.. "ammo left in your gun!")
	end
end)

What I'm trying to do is when you call the RemoteEvent pass in the player's mouse.Hit.

Example:

Tool.RemoteEvent:FireServer(Player:GetMouse().Hit)

Make sure you fire the event inside of a localscript.

This discussion is preserved as it was. New answers, comments, and votes are closed.