• Creating a money system
    5 replies, posted
Hello, another post about lua. I have little to no idea how to "script". I was wondering how I could script a money system. I've had experience in other scripting languages (Mostly Pawn, for the modification SA-MP, similar to C++ from what I've been told) but I don't understand a lot of this. I found a post on SQLite, and from what I've looked at, it comes default with gmod, so I don't need to download anything to make a saving/loading system. However, after looking at a tutorial (may be outdated) I tried to modify it and keep only stuff I need. I made it load up in init.lua, and included it in my cl_init for my hud so I can use the money global variable (I believe), but when I try to run it in-game I always get errors in consoles telling me I need to end the function or something. However, if I add another end, I usually get a different error, but when I searched, it said the error means I have an extra end. This is making me extremely angry, and I'd love it if someone could look over the script and tell me if I did something wrong. This is in a seperate .lua file in the gamemode folder in my gamemode. moneysystem.lua: [lua] function sql_value_stats ( ply ) steamid = sql.QueryValue("SELECT steamid FROM player_info WHERE steamid = '"..steamID.."'") money = sql.QueryValue("SELECT money FROM player_info WHERE steamid = '"..steamID.."'") ply:SetNWString("steamid", steamid) ply:SetNWInt("money", money) end function saveStat ( ply ) money = ply:GetNWInt("money") steamid = ply:GetNWString ("SteamID") sql.Query("UPDATE player_info SET money = "..money.." WHERE steamid = '"..steamid.."'") ply:ChatPrint("Stats updated!") end function tables_exist() if(!sql.TableExists("player_info")) then query = "CREATE TABLE player_info ( steamid varchar(255), money int )" result = sql.Query(query) if (sql.TableExists("player_info")) then Msg("Success! Table created!\n") end end end function new_player( SteamID, ply ) steamID = SteamID sql.Query( "INSERT INTO player_info (`steamid`, `money`)VALUES ('"..steamID.."', '100')" ) result = sql.Query( "SELECT steamid, money FROM player_info WHERE steamid = '"..steamID.."'" ) if (result) then Msg("Player account created !\n") sql_value_stats( ply ) else Msg("Something went wrong with creating a players info !\n") end end function player_exists( ply ) steamID = ply:GetNWString("SteamID") result = sql.Query("SELECT steamid, money FROM player_info WHERE steamid = '"..steamID.."'") if (result) then sql_value_stats( ply ) else new_player( steamID, ply ) end end function Initialize() tables_exist() end function PlayerInitialSpawn( ply ) timer.Create("Steam_id_delay", 1, 1, function() SteamID = ply:SteamID() ply:SetNWString("SteamID", SteamID) timer.Create("SaveStat", 10, 0, function() saveStat( ply ) end) player_exists( ply ) end) end hook.Add( "PlayerInitialSpawn", "PlayerInitialSpawn", PlayerInitialSpawn ) hook.Add( "Initialize", "Initialize", Initialize ) [/lua] cl_init.lua: [lua] include( 'shared.lua' ) include( 'moneysystem.lua' ) function GM:HUDPaint() local name = LocalPlayer():Name() local cash = LocalPlayer():GetNWInt("money") local health = math.Clamp(LocalPlayer():Health(),0,100) local armour = math.Clamp(LocalPlayer():Armor(),0,100) surface.CreateFont( "HUDFont", { font = "Trebuchet MS", size = ScreenScale(10), weight = 1000, antialias = false, shadow = false }) draw.RoundedBox(4, 10, ScrH() - 155, ScrW()/6, ScrH()/5.2, Color(0, 0, 0, 100)) draw.SimpleText("Name: " ..name, "HUDFont", 12, ScrH() - 145, Color(255,255,255,255)) draw.SimpleText("Money: " ..cash, "HUDFont", 12, ScrH() - 160, Color(255,255,255,255)) end function hidehud(name) for k, v in pairs({"CHudHealth", "CHudBattery", "CHudAmmo", "CHudSecondaryAmmo"}) do if name == v then return false end end end hook.Add("HUDShouldDraw", "HideOurHud:D", hidehud) [/lua] init.lua: [lua] AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile( "shared.lua" ) AddCSLuaFile( "moneysystem.lua" ) include( 'shared.lua' ) function GM:PlayerSpawn( ply ) self.BaseClass:PlayerSpawn( ply ) ply:SetGravity ( 1 ) ply:SetMaxHealth( 100, true ) ply:SetWalkSpeed( 190 ) ply:SetRunSpeed ( 235 ) end function GM:PlayerLoadout( ply ) ply:Give("weapon_physcannon") ply:SelectWeapon("weapon_physcannon") end function GM:PlayerInitialSpawn( ply ) ply:PrintMessage( HUD_PRINTTALK, "Welcome, " .. ply:Name() .. "!" ) end [/lua] shared.lua: [lua] GM.Name = "Roleplay" GM.Author = "Steven" GM.Email = "" GM.Website = "" DeriveGamemode( "sandbox" ) [/lua] If you don't want to look at the code and check what's wrong, I'd also love it if someone were to show me some simple loading/saving for a players steamid and money. Thanks, in advance, - Stevolas PS: Don't reply saying to google it or anything, I've already tried.
Post the error don't just say "got an error", most people won't just look through your entire gamemode for you.
> Entire gamemode > Not an entire gamemode But whatever. I'm not feeling like going on gmod right now to get the error, but I keep getting errors at line 26-35 saying I need an "end" to close the function. Every time I fix it, it gives me one somewhere else, or gives me an error almost exactly near it saying error <eof> or some crap, and after searching, it meant I needed to remove an extra end. Plus, like I said, nothing else is bugged, just the moneysystem.lua. I only included the other files incase you wanted to see how I use it.
[QUOTE=Stevolas;41606899]> Entire gamemode > Not an entire gamemode But whatever. I'm not feeling like going on gmod right now to get the error, but I keep getting errors at line 26-35 saying I need an "end" to close the function. Every time I fix it, it gives me one somewhere else, or gives me an error almost exactly near it saying error <eof> or some crap, and after searching, it meant I needed to remove an extra end. Plus, like I said, nothing else is bugged, just the moneysystem.lua. I only included the other files incase you wanted to see how I use it.[/QUOTE] we need the exact error if you're getting any help mr lazy.
[QUOTE=Stevolas;41606899]I'm not feeling like going on gmod right now to get the error[/QUOTE] Then why should we feel like helping you....
I just copied that entire text, pasted it in one file, auto-refreshed without the hook.Adds and overwriting my GM: functions ( I removed GM: ) and got no error. I'm not sure of the error, but it doesn't seem to be with what you posted in terms of a missing END. There are many ways to make a money system. You can use sv.db, sql lite, mysql, flat-file, etc. So what are you having problems with? We need the exact error in the exact script. Paste the full error with full script when you have that for us so we can help!
Sorry, you need to Log In to post a reply to this thread.