• Gamemode is still broken!!!!!!!!!!!!!!!!!!!!!!!!!!!
    1 replies, posted
Hello all, I am in the process of making a gmod gamemode where you get money for npc kills!!!!!! :D I got it to work BUUUUUT it gives you money whenever ANYONE kills an npc, is there any way to make it so that it will ONLY give you money if YOU kill the npc??? CODE: init.lua [CODE]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 GM:PlayerSpawn( ply ) ply:StripWeapons() ply:Give("weapon_crowbar") end 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 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(30, "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 GiveM4( ply ) cash = ply:GetMoney() if cash >= 300 then ply:TakeMoney( 300 ) ply:Give("weapon_m4") ply:GiveAmmo(75, "smg1") else ply:ChatPrint("You don't have enough money for an M4.") end end function BuyRifleAmmo( ply ) cash = ply:GetMoney() if cash >= 100 then ply:TakeMoney( 100 ) ply:GiveAmmo(100, "smg1") else ply:ChatPrint("You don't have enough money for rifle 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("buy_shotgun",GiveShotgun) concommand.Add("buy_shtg_ammo",GiveShtgAmmo) concommand.Add("buy_pist_ammo",GivePistAmmo) concommand.Add("buy_pistol",GivePistol) concommand.Add("buy_m4",GiveM4) function GM:OnNPCKilled( victim, killer, weapon ) RunConsoleCommand("NPCKill") end[/CODE] shared.lua [CODE]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 ) )[/CODE] cl_init.lua [CODE]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("100: Pistol") function BuyPistol:OnMousePressed() RunConsoleCommand("buy_pistol") end WList:AddItem(BuyPistol) local BuyM4 = vgui.Create("DButton") BuyM4:SetText("300: M4") function BuyM4:OnMousePressed() RunConsoleCommand("buy_m4") end WList:AddItem(BuyM4) local BuyRifleAmmo = vgui.Create("DButton") BuyRifleAmmo:SetText("100: Assult rifle ammo") function BuyRifleAmmo:OnMousePressed() RunConsoleCommand("buy_rif_ammo") end 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)[/CODE] sh_player.lua [CODE]local playerMeta = FindMetaTable("Player"); if (SERVER) then util.AddNetworkString("ply_SetMoney"); function playerMeta:SetMoney(amount) net.Start("ply_SetMoney"); net.WriteInt(amount, 16); net.Send(self); self.i_Money = amount; end; function playerMeta:AddMoney(amount) self:SetMoney(self:GetMoney() + amount); end; function playerMeta:TakeMoney(amount) self:AddMoney(-amount); end; function playerMeta:SaveMoney() local _, folder = find.Find("money", "DATA"); if (!folder[1]) then file.CreateDir("money"); end; local uniqueID = self:UniqueID(); local amount = self:GetMoney(); file.Write("money/"..uniqueID..".txt", amount); end; function playerMeta:RestoreMoney() local uniqueID = self:UniqueID(); local content = file.Read("money/"..uniqueID..".txt", "DATA"); if (content) then local amount = tonumber(content); if (amount) then self:SetMoney(amount); end; end; end; else net.Receive("ply_SetMoney", function(length) local amount = net.ReadInt(16); LocalPlayer().i_Money = amount; end); end; function playerMeta:GetMoney() return self.i_Money or 0; end;[/CODE] [highlight](User was banned for this post ("Making the same thread mulitple times, awful thread title" - postal))[/highlight]
[lua]function GM:OnNPCKilled( victim, killer, weapon ) RunConsoleCommand("NPCKill") end[/lua] Might want to specify which player runs the console command. killer:ConCommand( "NPCKill" )
Sorry, you need to Log In to post a reply to this thread.