So I made this gun where you can reload it and it connects to a GUI that shows the ammo amount but if I click to reload then unequip the gun it gives an error and doesn’t change the GUI and ammo amount and I get this error:
Players.ForgottenDogYT.Backpack.Ak 47.Server:85: attempt to index nil with ‘PlayerGui’
this is the code where it errors. also, this is a server script:
local function reload()
if reloading then return end
ReloadValue.Value = true
ReloadTrack = script.Parent.Parent.Humanoid:LoadAnimation(ReloadAnim)
ReloadTrack:Play()
CanFire = false
Handle.Reload:Play()
reloading = true
ReloadTrack.Stopped:wait()
Player = game:GetService("Players"):GetPlayerFromCharacter(Tool.Parent)
AmountGui = Player.PlayerGui:FindFirstChild("AmmoGui")
AmountGui.AmmoFrame.AmmoAmount.Text = 40
AmmoValue.Value = 40
Ammo = 40
reloading = false
CanFire = true
ReloadValue.Value = false
end
how can I make the GetPlayerFromCharacter(tool) also get it from the backpack? I’ve tried disabling the core GUI for backpacks but if i pick up a tool it also overrides the reload and breaks.