• need help getting money system working
    4 replies, posted
i need a money system that awards money on kills (which will be put it later, not now) but i have trouble even using this code (not mine) that has meta tables [CODE]local Player = FindMetaTable("Player") if !Player then return end function Player:GetMoney() return self.Money or 0 end function Player:CanAfford(num) return (self:GetMoney()-num) >= 0 end if SERVER then util.AddNetworkString("ply_Money") function Player:SetMoney(num, bank) self.Money = num net.Start("ply_Money") net.WriteEntity(self) net.WriteInt(num, 32) net.Broadcast() end function Player:AddMoney(num) self:SetMoney(self:GetMoney()+num) end else net.Receive("ply_Money", function(len) local ply = net.ReadEntity() local num = net.ReadInt(32) if !IsValid(ply) then return end ply.Money = num end) end [/CODE] i learnt abit of lua earlier today but i have no idea what metatables are, so this code, it sets up the money system right? so do i have money now? what is a "net.ReadInt"? is there anything else i need to put into the gamemode? and how do i see my cash or whatever? any help is appreciated.
[QUOTE=jellofacey;44285986]i need a money system that awards money on kills (which will be put it later, not now) but i have trouble even using this code (not mine) that has meta tables [CODE]local Player = FindMetaTable("Player") if !Player then return end function Player:GetMoney() return self.Money or 0 end function Player:CanAfford(num) return (self:GetMoney()-num) >= 0 end if SERVER then util.AddNetworkString("ply_Money") function Player:SetMoney(num, bank) self.Money = num net.Start("ply_Money") net.WriteEntity(self) net.WriteInt(num, 32) net.Broadcast() end function Player:AddMoney(num) self:SetMoney(self:GetMoney()+num) end else net.Receive("ply_Money", function(len) local ply = net.ReadEntity() local num = net.ReadInt(32) if !IsValid(ply) then return end ply.Money = num end) end [/CODE] i learnt abit of lua earlier today but i have no idea what metatables are, so this code, it sets up the money system right? so do i have money now? what is a "net.ReadInt"? is there anything else i need to put into the gamemode? and how do i see my cash or whatever? any help is appreciated.[/QUOTE] I'm assuming you got that from a post I made a while back, if you go back to that thread there should be another post with the same code with comments in it. [editline]19th March 2014[/editline] Found it: [URL="http://facepunch.com/showthread.php?t=1315437&p=42529130&viewfull=1#post42529130"]http://facepunch.com/showthread.php?t=1315437&p=42529130&viewfull=1#post42529130[/URL] [lua] local Player = FindMetaTable("Player") --Find the Players MetaTable if !Player then return end --If the MetaTable doesn't exist stop here function Player:GetMoney() --Calling this function will return your money, if money is nil then 0 will return return self.Money or 0 end function Player:CanAfford(num) return (self:GetMoney()-num) >= 0 end if SERVER then util.AddNetworkString("ply_Money") --Precache the net library message function Player:SetMoney(num) self:SetPData("money", num) --Save to SQLite database in the gmod base folder self.Money = num net.Start("ply_Money") --start net message net.WriteEntity(self) --write the player net.WriteInt(num, 32) net.Broadcast() --send the message to all players end function Player:AddMoney(num) self:SetMoney(self:GetMoney()+num) end local startMoney = 100 hook.Add("PlayerInitialSpawn", "sh_money.PlayerInitialSpawn", function(ply) timer.Simple(1, function() if !IsValid(ply) then return end ply:SetMoney(ply:GetPData("money", startMoney)) --If the PData of "money" is nil it will use startMoney as the value instead end) end) else net.Receive("ply_Money", function(len) local ply = net.ReadEntity() local num = net.ReadInt(32) if !IsValid(ply) then return end --If the player doesn't exist stop here ply.Money = num --set the value on the player object end) end [/lua]
[QUOTE=brandonj4;44286009]I'm assuming you got that from a post I made a while back, if you go back to that thread there should be another post with the same code with comments in it. [editline]19th March 2014[/editline] Found it: [URL="http://facepunch.com/showthread.php?t=1315437&p=42529130&viewfull=1#post42529130"]http://facepunch.com/showthread.php?t=1315437&p=42529130&viewfull=1#post42529130[/URL] [lua] local Player = FindMetaTable("Player") --Find the Players MetaTable if !Player then return end --If the MetaTable doesn't exist stop here function Player:GetMoney() --Calling this function will return your money, if money is nil then 0 will return return self.Money or 0 end function Player:CanAfford(num) return (self:GetMoney()-num) >= 0 end if SERVER then util.AddNetworkString("ply_Money") --Precache the net library message function Player:SetMoney(num) self:SetPData("money", num) --Save to SQLite database in the gmod base folder self.Money = num net.Start("ply_Money") --start net message net.WriteEntity(self) --write the player net.WriteInt(num, 32) net.Broadcast() --send the message to all players end function Player:AddMoney(num) self:SetMoney(self:GetMoney()+num) end local startMoney = 100 hook.Add("PlayerInitialSpawn", "sh_money.PlayerInitialSpawn", function(ply) timer.Simple(1, function() if !IsValid(ply) then return end ply:SetMoney(ply:GetPData("money", startMoney)) --If the PData of "money" is nil it will use startMoney as the value instead end) end) else net.Receive("ply_Money", function(len) local ply = net.ReadEntity() local num = net.ReadInt(32) if !IsValid(ply) then return end --If the player doesn't exist stop here ply.Money = num --set the value on the player object end) end [/lua][/QUOTE] oh thank you very much!
may i ask why doesnt [CODE]function print( Player:GetMoney() ) end[/CODE] work?
[QUOTE=jellofacey;44286483]may i ask why doesnt [CODE]function print( Player:GetMoney() ) end[/CODE] work?[/QUOTE] Because you're redefining a function by doing that, in addition to that, you're not declaring a variable as an argument but a function value with something which may not exist which may cause an error. If you over-write print, then use function print( ... ) deal with ...; end ... allows for unlimited arguments to be passed. I overwrite print in favor of using MsgC with different colors being used for different data-types. If you're only trying to print the data, simply : print( Player:GetMoney( ) );
Sorry, you need to Log In to post a reply to this thread.