Jump to content

Saving skins etc.


Recommended Posts

Hello, I'm thinking to save Wanted levels, player money, player skins, player weapons, cars in a database automaticly.

But where is the best place I can store it? MySQL? (and how) or a XML file or a plain text file (like lua or something).

And how can you get those things when a player disconnect? (Also for when a player have Network Trouble, because I have many players who have money and a tuned car, and they got disconnected, and when they reconnect their skin, weapons, money and car etc is resetted. What I want is to save it automaticly when a player disconnects, log out's, or MTA closes directly (or shut down the pc).

Link to comment

It all depends on how you're going to use the data saved. If you want to use MySQL you could make a website where users could log in and sell the cars, buy "new skins", buy weapons, etc. If you're not going to make a website then you can use SQLite or XML. One problem with MySQL/SQLite is that you have to think of a way to save cars.. It's not that simple especially if you haven't got much knowledge about SQL engines. Saving cars in XML files would be easy though.

To save all your stuff when player leaves use: onPlayerQuit

Link to comment
function createdb () 
        executeSQLCreateTable ( "ppplayers", "money INTEGER, health INTEGER, armour INTEGER, job TEXT, wanted INTEGER, player TEXT" ) 
end 
addCommandHandler ("dbadd", createdb ) 
  
  
function updatedb ( sourcePlayer, theTeam )  
    sourcename = getClientName ( sourcePlayer ) 
    player = executeSQLSelect ( "ppplayers", "player", "player = '" .. sourcename .. "'" ) 
        if ( player == false ) then 
            outputChatBox("You have no account to update", sourcePlayer) 
        else     
            executeSQLUpdate ( "ppplayers", "money = '800', health = '50', armour = '100', job = 'Police', wanted = '1'", "player = '" .. sourcename .. "'" ) 
            outputChatBox ("Update finished", sourcePlayer) 
        end 
end 
addCommandHandler ("updatedb", updatedb) 
  
  
  
  
function onjoindb ()  
local sourcename = getClientName ( source )  
        player = executeSQLSelect ( "ppplayers", "player", "player = '" .. sourcename .. "'" ) 
        if ( player == false ) then    
                executeSQLInsert ( "ppplayers", "'600', '100', '0', 'Civillian', '0', '" .. sourcename .. "'" ) 
                outputChatBox ( "This is your first time here! Welcome " .. sourcename .. "!", source ) 
        else 
                local money = executeSQLSelect ( "ppplayers", "money", "player = '" .. sourcename .. "'" ) 
                setPlayerMoney( source, money[1][1] ) 
                -- 
                local wanted = executeSQLSelect ( "ppplayers", "wanted", "player = '" .. sourcename .. "'" ) 
                setPlayerWantedLevel ( source, wanted[1][1] ) 
                -- 
                local job = executeSQLSelect ( "ppplayers", "job", "player = '" .. sourcename .. "'" ) 
                setPlayerTeam( source, getTeamFromName( job[1][1] ) ) 
                outputChatBox("Welcome back " .. sourcename .. " to the " .. job[1][1] .. " team... You have $" .. money[1][1], source) 
        end      
         
end 
  
addEventHandler ( "onPlayerJoin", getRootElement(), onjoindb ) 
  
function onquitdb () 
local sourcename = getClientName ( source ) 
        getmoney = getPlayerMoney ( source ) 
        getplayerTeam = getPlayerTeam ( source )  
        wanted = getPlayerWantedLevel( source ) 
        playerTeam = getTeamName ( getplayerTeam ) 
        executeSQLUpdate ( "ppplayers", "money = '" .. getmoney .. "', health = '50', armour = '100', job = '" .. playerTeam .. "', wanted = '" .. wanted .. "'", "player = '" .. sourcename .. "'" ) 
end 
addEventHandler ( "onPlayerQuit", getRootElement(), onquitdb )      
  

You may want to delete the useless information.. its very is moddable too.

Their may be a few bugs but this is just finished so.. dont rely on it too much

Link to comment
  
function dbadd () 
        executeSQLCreateTable ( "ppplayers", "money INTEGER, health INTEGER, armour INTEGER, job TEXT, wanted INTEGER, player TEXT" ) 
end 
addCommandHandler ("dbadd", dbadd) 
  
  
function updatedb ( sourcePlayer, theTeam )  
    sourcename = getClientName ( sourcePlayer ) 
    player = executeSQLSelect ( "ppplayers", "player", "player = '" .. sourcename .. "'" ) 
        if ( player == false ) then 
            outputChatBox("You have no account to update", sourcePlayer) 
        else     
            executeSQLUpdate ( "ppplayers", "money = '800', health = '50', armour = '100', job = 'Police', wanted = '1'", "player = '" .. sourcename .. "'" ) 
            outputChatBox ("Update finished", sourcePlayer) 
        end 
end 
addCommandHandler ("updatedb", updatedb) 
  
  
  
  
function onjoindb ()  
local sourcename = getClientName ( source )  
        player = executeSQLSelect ( "ppplayers", "player", "player = '" .. sourcename .. "'" ) 
        if ( player == false ) then    
                executeSQLInsert ( "ppplayers", "'600', '100', '0', 'Civillian', '0', '" .. sourcename .. "'" ) 
                outputChatBox ( "This is your first time here! Welcome " .. sourcename .. "!", source ) 
        else 
                local money = executeSQLSelect ( "ppplayers", "money", "player = '" .. sourcename .. "'" ) 
                setPlayerMoney( source, money[1][1] ) 
                -- 
                local wanted = executeSQLSelect ( "ppplayers", "wanted", "player = '" .. sourcename .. "'" ) 
                setPlayerWantedLevel ( source, wanted[1][1] ) 
                -- 
                local job = executeSQLSelect ( "ppplayers", "job", "player = '" .. sourcename .. "'" ) 
                setPlayerTeam( source, getTeamFromName( job[1][1] ) ) 
                outputChatBox("Welcome back " .. sourcename .. " to the " .. job[1][1] .. " team... You have $" .. money[1][1], source) 
        end      
         
end 
  
addEventHandler ( "onPlayerJoin", getRootElement(), onjoindb ) 
  
function onquitdb () 
local sourcename = getClientName ( source ) 
        getmoney = getPlayerMoney ( source ) 
        getplayerTeam = getPlayerTeam ( source )  
        wanted = getPlayerWantedLevel( source ) 
        playerTeam = getTeamName ( getplayerTeam ) 
        executeSQLUpdate ( "ppplayers", "money = '" .. getmoney .. "', health = '50', armour = '100', job = '" .. playerTeam .. "', wanted = '" .. wanted .. "'", "player = '" .. sourcename .. "'" ) 
end 
addEventHandler ( "onPlayerQuit", getRootElement(), onquitdb )      
    
function cash ( sourcePlayer ) 
    setPlayerMoney ( sourcePlayer, "50000") 
end 
addCommandHandler ("cash", cash) 
  

I keep getting error: db.lua line 33: attempt to call index field '?' (A nil value)

This is on MTA nightly and if anyone can help to clean up this script to mta 1 i will be grateful..

Thank you.

Link to comment

The wanted[1] doesn't exist. (line 34) And you're trying to get wanted[1][1], that means, take the first index from the nil value.

I'm not sure why tho.. Had something to do with rows and colums, but honestly, I don't know much about SQL(ite)

Link to comment

Why don't you debug it? Do some outputs and see why you get this error... It seems that executeSQLSelect doesn't return what it meant to. As I said, make some outputs and see why you get error.

Link to comment

I now know its

player = executeSQLSelect ( "ppplayers", "player", "player = '" .. sourcename .. "'" ) 
        if ( player == false ) then 

There has been no change to SQLselect recently and that has not been announced ? Sorry for the hassle.. Im just stuck

Link to comment
I now know its
player = executeSQLSelect ( "ppplayers", "player", "player = '" .. sourcename .. "'" ) 
        if ( player == false ) then 

There has been no change to SQLselect recently and that has not been announced ? Sorry for the hassle.. Im just stuck

If you haven't changed anything (inside the code and table structure) and you run it on nightly build then I guess executeSQLSelect needs some testing.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...