• Edit my Money System?
    16 replies, posted
Hello! i finished my money system and im having a tad of a problem with it. i beleive everything in the actual System is fine, yet i cant get my give-money-on-kill code working!(ex. moeny doesnt rise when you get a kill) heres the system: [lua] local ply = FindMetaTable("Player") local MoneyMod = {} local function InitialPlayerSpawn(pl) pl:SetNetworkedInt("money", 0) pl.Money = 0 pl.Salary = {} end hook.Add("PlayerInitialSpawn", "MoneyMod_PIS", InitialPlayerSpawn) local function ResetMoney() if SERVER then for k,v in pairs(player.GetAll()) do v:SendLua("LocalPlayer():SetNetworkedInt(\"money\", ".. v.Money ..")") end end end //timer.Create("ResetMoneys", 0.5, 0, ResetMoney) function ply:GetMoney() return self:GetNetworkedInt("money") end function ply:CanAfford(price) if SERVER then return self.Money >= price else return self:GetNetworkedInt("money") >= price end end if SERVER then function ply:SetMoney(amt) self.Money = amt end function ply:GiveMoney(amt) self.Money = self.Money + amt end function ply:TakeMoney(amt) self.Money = self.Money - amt end function ply:SetSalary(time, amt) self.Salary.Time = time self.Salary.Amt = amt timer.Create(self:Nick() .."'s Salary", self.Salary.Time, 0, function() self.Money = self.Money + self.Salary.Amt end) end function ply:HasSalary() if timer.IsTimer(self:Nick() .."'s Salary") then return true else return false end end function ply:ChangeSalary(time, amt) self.Salary.Time = time self.Salary.Amt = amt end function ply:RemoveSalary() if timer.IsTimer(self:Nick() .."'s Salary") then timer.Destroy(self:Nick() .."'s Salary") end end function SaveMoney(pl) file.Write(gmod.GetGamemode().Name .."/Money/".. string.gsub(pl:SteamID(), ":", "_") ..".txt", pl.Money) end function LoadMoney(pl) pl.Money = file.Read(gmod.GetGamemode().Name .."/Money/".. string.gsub(pl:SteamID(), ":", "_") ..".txt", pl.Money) end end --Money HUD below local function Money_Draw( ) local Cash = LocalPlayer():GetNWInt( "Money" ) draw.DrawText("Money: "..Cash, "KK", 60, 60, Color(0,255,0,255)) end hook.Add("HUDPaint", "MoneyPainting", Money_Draw) [/lua] and here is the small peice of code for the give-money [lua] function Plykill( victim, weapon, killer, ply) if victim:IsValid() and victim:IsPlayer() then killer:GiveMoney( 20 ) end end hook.Add( "PlayerDeath", "Plykill", Plykill ) [/lua] Please tell me what went WRONG?
I don't know what went wrong, but Im using this one system i found in a tut somewhere, I havent been able to test it yet: function KillCounter( victim, killer, weapon ) --Sets up a new function called KillCounter killer:SetNWInt("killcounter", killer:GetNWInt("killcounter") + 1) --Adds 1 everytime an NPC is killed. end hook.Add("OnNPCKilled","KillCounter", KillCounter)
[QUOTE=StartedBullet;24650646]I don't know what went wrong, but Im using this one system i found in a tut somewhere, I havent been able to test it yet: function KillCounter( victim, killer, weapon ) --Sets up a new function called KillCounter killer:SetNWInt("killcounter", killer:GetNWInt("killcounter") + 1) --Adds 1 everytime an NPC is killed. end hook.Add("OnNPCKilled","KillCounter", KillCounter)[/QUOTE] [url]http://www.garrysmod.org/downloads/?a=view&id=101388[/url]
money system is now working. only one problem though, it doesnt save to a database. how can i get it to save to a database?
ply:SetPData() Saves to sql! ply:GetPData() Loads from sql!
this is what i did: Added to Init.Lua [lua] 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", STARTINGMONEY) --Save it ply:SetMoney( STARTINGMONEY ) --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 fPlayerDisconnect( ply ) print("Player Disconnect: Money saved to SQLLite and TXT") ply:SaveMoney() ply:SaveMoneyTXT() end hook.Add( "PlayerDisconnected", "playerdisconnected", fPlayerDisconnect ) [/lua] Replaced SaveMoney and LoadMoney with: [lua] function ply:SaveMoney() local cash = self:GetMoney() self:SetPData("money", cash) end function ply:SaveMoneyTXT() file.Write(gmod.GetGamemode().Name .."/Money/".. string.gsub(self:SteamID(), ":", "_") ..".txt", self:GetMoneyString()) end end [/lua] AbsoLUTELY no change whatsoever! [editline]02:36AM[/editline] Shameles bump. [editline]02:36AM[/editline] shameless bump. again
You need to call SaveMoney And SaveMoneyText [lua] -- if that doesn't work for k,v in pairs(player.GetAll()) do v:SaveMoney() end timer.Create("Save", 1,0, function(ply) ply:SaveMoney() end,ply) [/lua] When you set the money you might wanna save it too.
Use the for he commented inside the timer
That is his method if you read his comment
Well i guess i cant exactly say its 100% right but a lua coder could of still used it as an explanation.
I knew it wouldn't work but why is this in the wiki, [lua] function Ignited( ply ) ply:Ignite(3,0) end timer.Create( "Atimer", 5, 1, Ignited, ply ) [/lua]
alright thanks a ton. one more thing, im having a bit of trouble with the money-saveing. for instance, if a player normally disconnects, the money is saved. if they accidentaly crash then it gets set back to the last save. same goes with a map change. my gamemode does alot of map changes, so this wont be good. i tried adding a save function to the end of the plykill function so the money is saved Every time you get more, but all this has done is TOTALY screwed with ti to a point that players dont GET any money for kills. i tried just adding the ply:SaveMoney() and the ply:SaveMoneyTXT() but nothing happened =\ [lua] function Plykill( victim, weapon, killer, ply) if victim:IsValid() and victim:IsPlayer() then killer:SetNWInt("Money", killer:GetNWInt("Money") + 20) killer:SaveMoney()--doesnt work killer:SaveMoneyTXT()--doesnt work -- i have also used ply:SaveMoney in both scenarios, no luck. end end [/lua] D:
Change [lua]function ply:SaveMoneyTXT() file.Write(gmod.GetGamemode().Name .."/Money/".. string.gsub(self:SteamID(), ":", "_") ..".txt", self:GetMoneyString()) end [/lua] to [lua]function ply:SaveMoneyTXT() print("Saved " ..gmod.GetGamemode().Name .."/Money/".. string.gsub(self:SteamID(), ":", "_") ..".txt" ) -- if this works then there should be no problem with the saving file.Write(gmod.GetGamemode().Name .."/Money/".. string.gsub(self:SteamID(), ":", "_") ..".txt", self:GetNWInt("money",0)) end [/lua]
Sorry, you need to Log In to post a reply to this thread.