• Need help with XP System
    1 replies, posted
I am making an xp system for my server and all the xp stuff works, the problem i have is that im trying to set my level when i have a certain amount of xp, but it wont work. I dont get any errors it just simply wont level me up when my xp is over 100. code: local meta = FindMetaTable("Player") function meta:addXP( amount ) local current_xp = self:GetXp() self:SetXp( current_xp + amount ) end function meta:SetXp( amount ) self:SetNetworkedInt( "Xp", amount ) self:SaveXp() end function meta:GetXp() return self:GetNetworkedInt( "Xp" ) end function meta:SaveXp() local experience = self:GetXp() self:SetPData( "xp", experience ) end function meta:SaveXpTXT() file.Write( gmod.GetGamemode().Name .."/Xp/".. string.gsub(self:SteamID(), ":", "_") ..".txt", self:GetXpString() ) end function meta:TakeXp( amount ) self:AddLevel( -amount ) end if (SERVER) then AddCSLuaFile( "xp-sas.lua" ) XP_STARTAMOUNT = 0 function xFirstSpawn( ply ) local experience = ply:GetPData( "xp" ) if experience == nil then ply:SetPData("xp", XP_STARTAMOUNT) ply:SetXp( XP_STARTAMOUNT ) else ply:SetXp( experience ) end end hook.Add( "PlayerInitialSpawn", "xPlayerInitialSpawn", xFirstSpawn ) function PrintXp( pl ) pl:ChatPrint( "Your xp is: " .. pl:GetXp() ) end function xPlayerDisconnect( ply ) print("Player Disconnect: Xp saved to SQLLite and TXT") ply:SaveXp() ply:SaveXpTXT() end concommand.Add( "xp_get", PrintXp ) end function AddCash(ply) ply:addXP(1.5) ply:ChatPrint( "You got 3 xp!" ) end concommand.Add("asdfghjkl142",AddCash) timer.Create( "Atimer", 60, 0, function(ply) RunConsoleCommand( "asdfghjkl142", ply, 1 ) end ) hook.Add("OnNPCKilled", "givemonez", function(npc, att, inf) att:addXP(10) att:ChatPrint( "You got 10 xp!" ) end) --For HUD use "tostring(LocalPlayer():GetXp())" ---------------------Level------------------------------ ----------------------------------------------------------- local meta = FindMetaTable("Player") function meta:AddLevel( amount ) local current_Level = self:GetLevel() self:SetLevel( current_Level + amount ) end function meta:SetLevel( amount ) self:SetNetworkedInt( "Level", amount ) self:SaveLevel() end function meta:GetLevel() return self:GetNetworkedInt( "Level" ) end function meta:SaveLevel() local eLevelerience = self:GetLevel() self:SetPData( "Level", eLevelerience ) end function meta:SaveLevelTXT() file.Write( gmod.GetGamemode().Name .."/Level/".. string.gsub(self:SteamID(), ":", "_") ..".txt", self:GetLevelString() ) end function meta:TakeLevel( amount ) self:AddLevel( -amount ) end if (SERVER) then AddCSLuaFile( "Level-sas.lua" ) Level_STARTAMOUNT = 1 function xFirstSpawn( ply ) local eLevelerience = ply:GetPData( "Level" ) if eLevelerience == nil then ply:SetPData("Level", Level_STARTAMOUNT) ply:SetLevel( Level_STARTAMOUNT ) else ply:SetLevel( eLevelerience ) end end hook.Add( "PlayerInitialSpawn", "LevellayerInitialSpawn", xFirstSpawn ) function PrintLevel( pl ) pl:ChatPrint( "Your Level is: " .. pl:GetLevel() ) end function LevellayerDisconnect( ply ) print("Player Disconnect: Level saved to SQLLite and TXT") ply:SaveLevel() ply:SaveLevelTXT() end concommand.Add( "Level_get", PrintLevel ) end function meta:AddLevelpls() self:AddLevel(1) self:SetXp(0) end function meta:AddLevels( ... ) if(tonumber(self:GetNetworkedInt("Xp")) > 99 ) then self:AddLevelpls() end end concommand.Add( "addlevel", AddLevelpls )
Few things: Wrap your code in [CODE] or [lua] tags when you post it in the forums Indent properly, your code is a mess Your file looks like 2 files that were merged together, you have a lot of repetitions that are pointless (e.g. [B]local meta = FindMetaTable("Player")[/B] on line 83) Are you letting the players add levels and XP with a client-side console command? Bad idea Use SetNWVar/GetNWVar instead of SetNetworkedVar/GetNetworkedVar I'd use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/engine/ActiveGamemode]engine.ActiveGamemode[/url] rather than [B]gmod.GetGamemode().Name[/B] GetLevelString (Which you call on line 105) is undefined. But the [B]SaveLevelTXT[/B] function itself is never called You save the player's level/xp in both a text file and PData, but only use PData. So you're writing for nothing As for the problem you mentioned: No part of the code makes you level up automatically when your XP is over 100, do you mean you run the "addlevel" console command and nothing happens? Where is the file located? It looks like it should be shared but a lot of the functions you define are pointless on the client This code is a [B]mess[/B]. I think it'd be better to just make it from scratch
Sorry, you need to Log In to post a reply to this thread.