So I have this gun script and I want the ammo GUI to pop up when the player quips the gun but I get this error:
Players.ForgottenDogYT.Backpack.Ak.Server:32: attempt to index nil with ‘PlayerGui’
This is the script where it errors:
local Player = game.Players.LocalPlayer
local Ammo = 40
local reloading = false
local Tool = script.Parent
local Handle = Tool.Handle
local MouseEvent = Tool.MouseEvent
local FirePointObject = Handle.GunFirePoint
local FastCast = require(Tool.FastCastRedux)
local FireSound = Handle.Fire
local ImpactParticle = Handle.ImpactParticle
local Debris = game:GetService("Debris")
local table = require(Tool.FastCastRedux.Table)
local PartCacheModule = require(Tool.PartCache)
local AmountGui = Player.PlayerGui:FindFirstChild("AmmoGui")
local CanFire = true -- Used for a cooldown.
local RNG = Random.new() -- Set up a randomizer.
local TAU = math.pi * 2 -- Set up mathematical constant Tau (pi * 2)
FastCast.DebugLogging = DEBUG
FastCast.VisualizeCasts = DEBUG
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
--AmmoStuff
Tool.Equipped:Connect(function()
AmountGui.AmmoFrame.Visible = true
AmountGui.AmmoFrame.AmmoAmount.Text = Ammo
end)
Tool.Unequipped:Connect(function()
AmountGui.AmmoFrame.Visible = false
end)
As you cannot use LocalPlayer to get the player, since it is from the server, you have to use GetPlayerFromCharacter.
local Ammo = 40
local reloading = false
local Tool = script.Parent
local Handle = Tool.Handle
local MouseEvent = Tool.MouseEvent
local FirePointObject = Handle.GunFirePoint
local FastCast = require(Tool.FastCastRedux)
local FireSound = Handle.Fire
local ImpactParticle = Handle.ImpactParticle
local Debris = game:GetService("Debris")
local table = require(Tool.FastCastRedux.Table)
local PartCacheModule = require(Tool.PartCache)
local CanFire = true
local RNG = Random.new()
local TAU = math.pi * 2
FastCast.DebugLogging = DEBUG
FastCast.VisualizeCasts = DEBUG
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
--AmmoStuff
local Player = nil
local AmountGui = nil
Tool.Equipped:Connect(function()
Player = game:GetService("Players"):GetPlayerFromCharacter(Tool.Parent)
if Player then
AmountGui = Player.PlayerGui:FindFirstChild("AmmoGui")
AmountGui.AmmoFrame.Visible = true
AmountGui.AmmoFrame.AmmoAmount.Text = Ammo
end
end)
Tool.Unequipped:Connect(function()
if AmountGui then AmountGui.AmmoFrame.Visible = false end
end)
it worked to show the gui but how can i add it for other parts of my script? because i have this:
if Ammo >= 1 and not reloading then
and this:
local function reload()
if reloading then return end
CanFire = false
Handle.Reload:Play()
reloading = true
wait(3)
Ammo = 30
reloading = false
CanFire = true
end