• Killing other people
    15 replies, posted
i have a money system on my server, and i am trying to write a bit in the money system that makes it so that, if you kill another player, you get 100 money this is the money system code, if someone could point me in the right direction that would be cool. [lua] local STARTMONEY = 0 local PAYDAYS = false local PAYDAY_AMOUNT = 5 local PAYDAY_INTERVAL = 8 local PAYDAY_SOUND = true local MONEY_HUD = true if SERVER then local pm = FindMetaTable("Player") function PlayerMoneySpawn( ply ) ply:Money_Create() end hook.Add("PlayerInitialSpawn", "MoneySystemStart", PlayerMoneySpawn) function pm:Money_Create() -- DONT TOUCH! THIS FUNCTION SAVES THE SHIT print("Creating money account for: "..self:Nick()) if self:GetPData( "cash" ) == nil then self:SetPData( "cash", STARTMONEY ) end end function pm:Money_Set( cash ) self:SetPData( "cash", tonumber(cash) ) self:SetNWInt( "cash", tonumber(cash) ) end function pm:Money_Add( cash ) if cash > 0 then local current = tonumber(self:GetPData( "cash" )) self:Money_Set( current + cash ) end end function pm:Money_Has( cash ) local current = tonumber(self:GetPData( "cash" )) if current >= tonumber(cash) then return true else return false end end function pm:Money_Get() local current = tonumber(self:GetPData( "cash" )) return current end function pm:Money_Take( cash ) if self:Money_Has( cash ) then local current = tonumber(self:GetPData( "cash" )) self:Money_Set( current - cash ) end end if PAYDAYS then timer.Create( "payday_timer", PAYDAY_INTERVAL, 0, function() for k, ply in pairs(player.GetAll()) do ply:Money_Add( PAYDAY_AMOUNT ) if PAYDAY_SOUND then ply:EmitSound("items/ammo_pickup.wav") end end end ) end else local function Money_Draw() if not MONEY_HUD then return end local mulleh = LocalPlayer():GetNWInt( "cash" ) draw.DrawText("Yen: "..mulleh, "Trebuchet18", 5, 5, Color(255,215,0,255)) end hook.Add("HUDPaint", "MoneyPainting", Money_Draw) end[/lua]
[lua] function GM:DoPlayerDeath( ply, attacker, dmginfo ) if ( attacker:IsValid() && attacker:IsPlayer() ) then ply: (player who got killed) attacker: (person who killed ply) end end[/lua] [editline]08:10PM[/editline] [url]http://wiki.garrysmod.com/?title=Gamemode.DoPlayerDeath[/url]
[QUOTE=jeff223;22403507][lua] function GM:DoPlayerDeath( ply, attacker, dmginfo ) if ( attacker:IsValid() && attacker:IsPlayer() ) then ply: (player who got killed) attacker: (person who killed ply) end end[/lua] [editline]08:10PM[/editline] [url]http://wiki.garrysmod.com/?title=Gamemode.DoPlayerDeath[/url][/QUOTE] so would this work you think? [lua]function GM:DoPlayerDeath( ply, attacker, dmginfo ) ply:CreateRagdoll() ply:AddDeaths( 1 ) if ( attacker:IsValid() && attacker:IsPlayer() ) then if ( attacker == ply ) then attacker:AddFrags( -1 ) attacker:pm:Money_Add( -1 ) else attacker:AddFrags( 1 ) attacker:pm:Money_Add( 100 ) end end end [/lua]
It was example. This isn't the requests thread. Don't be dumb, Newbrict. ***EDIT***: Sorry... read 2 posts below.
ok here is my current code, whenever i kill someone it doesn't give me 100 =.= [lua] local STARTMONEY = 0 local PAYDAYS = false local PAYDAY_AMOUNT = 5 local PAYDAY_INTERVAL = 8 local PAYDAY_SOUND = true local MONEY_HUD = true if SERVER then local pm = FindMetaTable("Player") function PlayerMoneySpawn( ply ) ply:Money_Create() end hook.Add("PlayerInitialSpawn", "MoneySystemStart", PlayerMoneySpawn) function pm:Money_Create() -- DONT TOUCH! THIS FUNCTION SAVES THE SHIT print("Creating money account for: "..self:Nick()) if self:GetPData( "cash" ) == nil then self:SetPData( "cash", STARTMONEY ) end end function pm:Money_Set( cash ) self:SetPData( "cash", tonumber(cash) ) self:SetNWInt( "cash", tonumber(cash) ) end function pm:Money_Add( cash ) if cash > 0 then local current = tonumber(self:GetPData( "cash" )) self:Money_Set( current + cash ) end end function GM:DoPlayerDeath( ply, attacker, dmginfo ) ply:CreateRagdoll() ply:AddDeaths( 1 ) -- here HERE here if ( attacker:IsValid() && attacker:IsPlayer() ) then if ( attacker == ply ) then attacker:AddFrags( -1 ) else attacker:AddFrags( 1 ) a = 100 attacker:Money_Set( current + a ) end end end function pm:Money_Has( cash ) local current = tonumber(self:GetPData( "cash" )) if current >= tonumber(cash) then return true else return false end end function pm:Money_Get() local current = tonumber(self:GetPData( "cash" )) return current end function pm:Money_Take( cash ) if self:Money_Has( cash ) then local current = tonumber(self:GetPData( "cash" )) self:Money_Set( current - cash ) end end if PAYDAYS then timer.Create( "payday_timer", PAYDAY_INTERVAL, 0, function() for k, ply in pairs(player.GetAll()) do ply:Money_Add( PAYDAY_AMOUNT ) if PAYDAY_SOUND then ply:EmitSound("items/ammo_pickup.wav") end end end ) end else local function Money_Draw() if not MONEY_HUD then return end local mulleh = LocalPlayer():GetNWInt( "cash" ) draw.DrawText("Yen: "..mulleh, "Trebuchet18", 5, 5, Color(255,215,0,255)) end hook.Add("HUDPaint", "MoneyPainting", Money_Draw) end[/lua]
Replace attacker:Money_Set( current + a ) with [code]attacker:SetPData("cash", attacker:GetPData("cash") + a )[/code]May or may not work. Not completely sure why his didn't, so idk... [editline]09:47PM[/editline] LOL, oops! sorry about calling you dumb. I thought you were someone else coming on here and saying like "NYAH, THAT'S NOT GOING TO WORK" and I was showing an example...
[QUOTE=jeff223;22405177]Replace attacker:Money_Set( current + a ) with [code]attacker:SetPData("cash", attacker:GetPData("cash") + a )[/code]May or may not work. Not completely sure why his didn't, so idk... [editline]09:47PM[/editline] LOL, oops! sorry about calling you dumb. I thought you were someone else coming on here and saying like "NYAH, THAT'S NOT GOING TO WORK" and I was showing an example...[/QUOTE] no problem, and also your fix didn't work :( [editline]01:17AM[/editline] anyone else have an idea?
problem still not fixed,i also tried this [lua] function GM:DoPlayerDeath( ply, attacker, dmginfo ) ply:CreateRagdoll() ply:AddDeaths( 1 ) if ( attacker:IsValid() && attacker:IsPlayer() ) then if ( attacker == ply ) then attacker:AddFrags( -1 ) else attacker:AddFrags( 1 ) local current = tonumber(self:GetPData( "cash" )) a = 100 attacker:SetPData("cash", current + a ) end end end[/lua] i think i am noticing something though i don't beleive i have ply defined as anything, im not sure how i would fix this though :X
[lua] function GM:DoPlayerDeath( ply, attacker, dmginfo ) ply:CreateRagdoll() ply:AddDeaths( 1 ) if ( attacker:IsValid() && attacker:IsPlayer() ) then if ( attacker == ply ) then attacker:AddFrags( -1 ) else attacker:AddFrags( 1 ) local current = tonumber(self:GetPData( "cash" )) local a = 100 attacker:Money_Add( 100 ) end end end[/lua] this is what i have now, still no luck, still not resolved, still needing help.
Just use [b][url=wiki.garrysmod.com/?title=Gamemode.PlayerDeath]Gamemode.PlayerDeath [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] instead.
is this proper usage? [lua]function playerDies( victim, weapon, killer ) Msg( "Player " .. victim:GetName() .. " has died."\n ) killer:Money_Add( 100 ) end hook.Add( "PlayerDeath", "playerDeathTest", playerDies )[/lua]
if you're doing that serverside, it's best to do this [lua]function Someshit( victim, weapon, killer ) Msg( "Player " .. victim:GetName() .. " has died."\n ) killer:Money_Add( 100 ) end hook.Add('DoPlayerDeath', 'Someshit', Someshit)[/lua] Otherwise, do your way.
Only do that if your coding the Gamemode, not if its a gamemode not by you. [lua] function playerDies( victim, weapon, killer ) Msg( "Player " .. victim:GetName() .. " has died. \n" ) -- \n goes inside the string if(killer:IsPlayer()) then killer:Money_Add( 100 ) end -- You have to check if the killer is a player right? end hook.Add( "PlayerDeath", "playerDeathTest", playerDies ) [/lua]
I've almost given up hope. People trying to help but not understanding what a hook is - Lucky Luke
PROBLEM SOLVED, i now receive money when i kill someone, the only problem is when i rejoin the server it displays 0 money until i kill someone, then it displays what i really have, anyway to fix this?
Run Money_Set to the current money on initial spawn, should do it
Sorry, you need to Log In to post a reply to this thread.