Thehookerkiller01 Posted June 18, 2009 Share Posted June 18, 2009 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
50p Posted June 18, 2009 Share Posted June 18, 2009 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
knash94 Posted June 18, 2009 Share Posted June 18, 2009 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
Thehookerkiller01 Posted June 22, 2009 Author Share Posted June 22, 2009 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 Thx man Edit: should I have to type every time that the server start dbadd or not? Edit2: why health 50? Link to comment
knash94 Posted June 22, 2009 Share Posted June 22, 2009 I aint fixed it up for regular use and it was 50 health for when i was testing it also you wont need to put dbadd evry time.. if you wish you can change the handler for when resource starts. ( But it dont make no diffrence ) Link to comment
Thehookerkiller01 Posted June 27, 2009 Author Share Posted June 27, 2009 [19:11:24] ERROR: script.lua:30: attempt to index field '?' (a nil value) Line 30: setPlayerMoney( source, money[1][1] ) Link to comment
knash94 Posted July 5, 2009 Share Posted July 5, 2009 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
Thehookerkiller01 Posted July 5, 2009 Author Share Posted July 5, 2009 I'm using the nightly version.. Link to comment
knash94 Posted July 6, 2009 Share Posted July 6, 2009 Sorry to bump but i need to start moving on with some scripts and this needs to be fixed before i can continue... Anyone knows what the problem is? works like a dream in DP2.3 tho ;\ Link to comment
Lordy Posted July 6, 2009 Share Posted July 6, 2009 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
knash94 Posted July 6, 2009 Share Posted July 6, 2009 Is that why it works in DP2.3? But thanks for trying to help (Y) Link to comment
50p Posted July 6, 2009 Share Posted July 6, 2009 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
Thehookerkiller01 Posted July 7, 2009 Author Share Posted July 7, 2009 Already had sent the error. Link to comment
knash94 Posted July 7, 2009 Share Posted July 7, 2009 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
50p Posted July 7, 2009 Share Posted July 7, 2009 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now