Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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)

0
Tool.Parent.Parent.mouse Tyler090130 619 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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.

Ad

Answer this question