How to give shotgun ammo?

Hello all, I am in the process of making a dm gamemode, and I ran into a recent problem. I need to add a command that adds shotgun ammo. I have the command setup right because I have a print command in there an it works fine, only the ammo part doesn’t. Here’s my code for the GiveAmmo:

ply:GiveAmmo(80, “shotgun”)
ply:ChatPrint(“You have been given 80 shotgun ammo.”)

There is no error, it just simply doesn’t give the player the ammo. THANK YOU!!!

– Kimzey

Unless “shotgun” ammo is created by you that’s the problem, try replacing “shotgun” with “Buckshot”.

Btw, you should probably ask questions like this here in the future.

Alright, thanks, it worked. One more problem, I made a vgui button, and made it pop up in a dropdown menu, but it constantly shows upin the top left hand corner of my screen, any help?

I’m really crappy at lua but by the sounds of that you possibly haven’t got the dropdown parented properly? Post the code here if you still can’t figure it out.

shared.lua:


GM.Name = "First Gamemode"
GM.Author = "=YRS= Cwkimzey"
GM.Email = "cwkimzey@gmail.com"
GM.Website = "www.www.com.com"

DeriveGamemode( "sandbox" )

team.SetUp(1, "Red", Color( 255, 0, 0, 255 ) )
team.SetUp(2, "Blue", Color( 0, 0, 255, 255 ) )
team.SetUp(3, "Spectators", Color( 25, 25, 25, 25 ) )

init.lua:


AddCSLuaFile( "shared.lua" )
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "sh_player.lua" )
include( "sh_player.lua" )

include( "shared.lua" )

MONEY_STARTAMOUNT = 1000 --Can be changed to your starting amount
 
function FirstSpawn( ply )
	local cash = ply:GetPData("money") --Get the saved money amount
 
	if cash == nil then --If it doesn't exist supply the player with the starting money amount
		ply:SetPData("money", MONEY_STARTAMOUNT) --Save it
		ply:SetMoney( MONEY_STARTAMOUNT ) --Set it to the networked ints that can be called from the client too
	else
	ply:SetMoney( cash ) --If not, set the networked ints to what we last saved
	end
 
end
hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", FirstSpawn )
 
function PrintCash( pl )
	pl:ChatPrint("Your cash is: " .. pl:GetMoney())
end
 
function fPlayerDisconnect( ply )
	print("Player Disconnect: Money saved to SQLLite and TXT")
	ply:SaveMoney()
	ply:SaveMoneyTXT()
end
 
concommand.Add("cash_get",PrintCash)
 
	
function GM:PlayerInitialSpawn( ply )
	ply:SetTeam( 1 )
end

function NPCKill( ply )
	ply:AddMoney( 10 )
	ply:ChatPrint("You killed an NPC, and gained 10 dollars!")
end

function GiveWeapPistol( ply )
	cash = ply:GetMoney()
	if cash >= 100 then
		ply:TakeMoney( 100 )
		ply:Give("weapon_pistol")
		ply:GiveAmmo(25, "pistol")
	else
	ply:ChatPrint("You don't have enough money for a pistol.")
	end
end

function GM:ShowHelp( ply ) -- This hook is called everytime F1 is pressed.
    umsg.Start( "MyMenu", ply ) -- Sending a message to the client.
    umsg.End()
end --Ends function

function MyMenu( ply ) --Start the function
    umsg.Start( "MyMenu", ply ) --Send the contents of "MyMenu" to the client
    umsg.End()
end --End the function
hook.Add("ShowHelp", "MyHook", MyMenu) --Add the hook "ShowHelp" so it opens with F1

function GiveShtgAmmo( ply )
	cash = ply:GetMoney()
	if cash >= 50 then
		ply:TakeMoney( 50 )
		ply:GiveAmmo(80, "Buckshot")
		ply:ChatPrint("Gave you 80 shotgun ammo.")
	else
	ply:ChatPrint("You don't have enough money for shotgun ammo.")
	end
end

function GivePistol( ply )
	cash = ply:GetMoney()
	if cash >= 80 then
		ply:TakeMoney( 80 )
		ply:Give("weapon_pistol")
		ply:GiveAmmo(100, "pistol")
	else
	ply:ChatPrint("You don't have enough money for a pistol.")
	end
end

function GivePistAmmo( ply )
	cash = ply:GetMoney()
	if cash >= 50 then
		ply:TakeMoney( 50 )
		ply:GiveAmmo(100, "pistol")
		ply:ChatPrint("Gave you 100 spistol ammo.")
	else
	ply:ChatPrint("You don't have enough money for pistol ammo.")
	end
end

function GiveShotgun( ply )
	cash = ply:GetMoney()
	if cash >= 150 then
		ply:TakeMoney( 150 )
		ply:Give("weapon_shotgun")
		ply:GiveAmmo(20, "Buckshot")
	else
	ply:ChatPrint("You don't have enough money for a shotgun.")
	end
end

concommand.Add("NPCKill",NPCKill)
concommand.Add("give_weap_pistol",GiveWeapPistol)
concommand.Add("buy_shotgun",GiveShotgun)
concommand.Add("buy_shtg_ammo",GiveShtgAmmo)
concommand.Add("buy_pist_ammo",GivePistAmmo)
concommand.Add("buy_pistol",GivePistol)

function GM:OnNPCKilled( victim, killer, weapon )
   RunConsoleCommand("NPCKill")
end

function GM:PlayerLoadout( ply )
	if ply:Team() == 1 then
	ply:Give("weapon_pistol")
	ply:GiveAmmo(100, "pistol")
	
	else
	
	ply:Give("weapon_pistol")
	ply:GiveAmmo(100, "pistol")
	end
end

cl_init.lua:


include( "shared.lua" )
include( "sh_player.lua" )
local function MyMenu()
    local Menu = vgui.Create("DFrame")
    Menu:SetPos(10, 10)
    Menu:SetSize(800, 350)
    Menu:SetText("My Menu")
    Menu:SetDraggable(false)
    Menu:ShowCloseButton(true)
    Menu:MakePopup()
	
	local Ammo = vgui.Create("DCollapsibleCategory", Menu)
	Ammo:SetPos(20, 40)
	Ammo:SetLabel("Weapons")
	Ammo:SetSize(350, 20)
	Ammo:SetExpanded(0)
	
	local Col = vgui.Create("DCollapsibleCategory", Menu)
	Col:SetPos(380, 40)
	Col:SetLabel("Ammo")
	Col:SetSize(350, 20)
	Col:SetExpanded(0)
	
	local WList = vgui.Create( "DPanelList" )
	WList:SetAutoSize(true)
	WList:SetSpacing(5)
	WList:EnableVerticalScrollbar(true)
	
	local AList = vgui.Create( "DPanelList" )
	AList:SetAutoSize(true)
	AList:SetSpacing(5)
	AList:EnableVerticalScrollbar(true)
	
	Col:SetContents(AList)
	Ammo:SetContents(WList)
	
	local BuyShotgun = vgui.Create("DButton")
    BuyShotgun:SetText("Shotgun")
	function BuyShotgun:OnMousePressed()
		RunConsoleCommand("buy_shotgun")
	end
	WList:AddItem(BuyShotgun)
	
	local BuyPistol = vgui.Create("DButton")
    BuyPistol:SetText("Pistol")
	function BuyShotgun:OnMousePressed()
		RunConsoleCommand("buy_pistol")
	end
	WList:AddItem(BuyPistol)
	
	local BuyPistolAmmo = vgui.Create("DButton")
	BuyPistolAmmo:SetText("Pistol ammo")
	function BuyPistolAmmo:OnMousePressed()
		RunConsoleCommand("buy_pist_ammo")
	end
	AList:AddItem(BuyPistolAmmo)
	
	local BuyShotgunAmmo = vgui.Create("DButton")
	BuyShotgunAmmo:SetText("Shotgun Ammo")
	function BuyShotgunAmmo:OnMousePressed()
		RunConsoleCommand("buy_shtg_ammo")
	end
	AList:AddItem(BuyShotgunAmmo)
	
end

usermessage.Hook("MyMenu", MyMenu)

sh_player.lua:


local meta = FindMetaTable("Player") --Get the meta table of player
 
function meta:AddMoney(amount)
	local current_cash = self:GetMoney()
	self:SetMoney( current_cash + amount )
end
 
function meta:SetMoney(amount)
	self:SetNetworkedInt( "Money", amount )
	self:SaveMoney()
end
 
function meta:SaveMoney()
	local cash = self:GetMoney()
	self:SetPData("money", cash)
end
 
function meta:SaveMoneyTXT()
	file.Write(gmod.GetGamemode().Name .."/Money/".. string.gsub(self:SteamID(), ":", "_") ..".txt", self:GetMoneyString())
end
 
function meta:TakeMoney(amount)
   --Add money function here
   self:AddMoney(-amount)
end
 
function meta:GetMoney()
	return self:GetNetworkedInt( "Money" )
end
 

By what you said I expected a button to be left floating in the top left corner but this is what I have:
http://puu.sh/1xYgl

It looks okay to me? The only problem I can see is that the actual derma panel doesn’t seem to position itself properly, it’s in the top left by default and if Menu:Center() is used it’s again not positioned correctly. I don’t know what is causing it and won’t bother to assist you anymore because it’ll be like the blind leading the blind, sorry. :v:

The button is floating the VERY top left of the WHOLE screen, not just the vgui.

Just saying, you shouldn’t be using hooks in a game mode, that is for addons and other stuff.