bartje01 Posted March 6, 2011 Share Posted March 6, 2011 Hey guys,. Sorry that I'm asking something again but I want to save money but it doesn't work I've got no errors function onPlayerQuit ( ) -- when a player leaves, store his current money amount in his account data local playeraccount = getPlayerAccount ( source ) if ( playeraccount ) then local playermoney = getPlayerMoney ( source ) setAccountData ( playeraccount, "savemoney.money", playermoney ) end end function onPlayerLogin ( ) -- when a player joins, retrieve his money amount from his account data and set it local playeraccount = getPlayerAccount ( source ) if ( playeraccount ) then local playermoney = getAccountData ( playeraccount, "savemoney.money" ) -- make sure there was actually a value saved under this key (check if playermoney is not false). -- this will for example not be the case when a player plays the gametype for the first time if ( playermoney ) then setPlayerMoney ( source, playermoney ) end end end addEventHandler ( "onPlayerQuit", getRootElement ( ), onPlayerQuit ) addEventHandler ( "onPlayerLogin", getRootElement ( ), onPlayerLogin ) Please help Link to comment
proracer Posted March 6, 2011 Share Posted March 6, 2011 When a player logins it can't retrieve the account data because it is only set when he quits from server so you should try this: function onPlayerQuit ( ) -- when a player leaves, store his current money amount in his account data local playeraccount = getPlayerAccount ( source ) if ( playeraccount ) then local playermoney = getPlayerMoney ( source ) setAccountData ( playeraccount, "savemoney.money", playermoney ) end end function onPlayerLogin ( ) -- when a player joins, retrieve his money amount from his account data and set it local playeraccount = getPlayerAccount ( source ) if ( playeraccount ) then local playermoney = getAccountData ( playeraccount, "savemoney.money" ) -- make sure there was actually a value saved under this key (check if playermoney is not false). -- this will for example not be the case when a player plays the gametype for the first time if ( playermoney ) then setPlayerMoney ( source, playermoney ) else setPlayerMoney ( source, 0 ) -- if it cant retrieve data set it to 0 end end end addEventHandler ( "onPlayerQuit", getRootElement ( ), onPlayerQuit ) addEventHandler ( "onPlayerLogin", getRootElement ( ), onPlayerLogin ) Link to comment
bartje01 Posted March 6, 2011 Author Share Posted March 6, 2011 Doesn't have effect again Link to comment
Kenix Posted March 6, 2011 Share Posted March 6, 2011 Hey guys,. Sorry that I'm asking something again but I want to save money but it doesn't workI've got no errors function onPlayerQuit ( ) -- when a player leaves, store his current money amount in his account data local playeraccount = getPlayerAccount ( source ) if ( playeraccount ) then local playermoney = getPlayerMoney ( source ) setAccountData ( playeraccount, "savemoney.money", playermoney ) end end function onPlayerLogin ( ) -- when a player joins, retrieve his money amount from his account data and set it local playeraccount = getPlayerAccount ( source ) if ( playeraccount ) then local playermoney = getAccountData ( playeraccount, "savemoney.money" ) -- make sure there was actually a value saved under this key (check if playermoney is not false). -- this will for example not be the case when a player plays the gametype for the first time if ( playermoney ) then setPlayerMoney ( source, playermoney ) end end end addEventHandler ( "onPlayerQuit", getRootElement ( ), onPlayerQuit ) addEventHandler ( "onPlayerLogin", getRootElement ( ), onPlayerLogin ) Please help use this function playerLogin (thePreviousAccount, theCurrentAccount, autoLogin) if not (isGuestAccount (getPlayerAccount (source))) then local accountData = getAccountData (theCurrentAccount, "savemoney.money") if (accountData) then local playerMoney = getAccountData (theCurrentAccount, "savemoney.money") setPlayerMoney (source, playerMoney) end end end addEventHandler ("onPlayerLogin", getRootElement(), playerLogin) function onQuit (quitType, reason, responsibleElement) if not (isGuestAccount (getPlayerAccount (source))) then account = getPlayerAccount (source) if (account) then setAccountData (account, "savemoney.money", tostring (getPlayerMoney (source))) end end end addEventHandler ("onPlayerQuit", getRootElement(), onQuit) Link to comment
bartje01 Posted March 6, 2011 Author Share Posted March 6, 2011 Damn. Still doesn't work I'm using the race script of MTA by the way. I put this in my race_server.lua Link to comment
bamby12 Posted March 6, 2011 Share Posted March 6, 2011 i don't think this will work in race because the race game mode will spawn your player at the start of the race and the maps change all the time so you could spawn your player outside of the map if you get what im trying to explain, in other words if you do manage to get this to work then say i join your server on map A and logout then it will save my position and then if map B started then ill spawn in the same place where i was on map A and i would be out of the race. theres probably another way to do this but i don't know how you would do it whilst the maps are changing sorry. Link to comment
bartje01 Posted March 6, 2011 Author Share Posted March 6, 2011 @ Bamby. We're talking about money saving here:P Link to comment
proracer Posted March 6, 2011 Share Posted March 6, 2011 setPlayerMoney doesn't work in Race mode ... You must create element data for cash by yourself. Link to comment
bartje01 Posted March 6, 2011 Author Share Posted March 6, 2011 Aaah proracer. I see. I already noticed that there was no money bar in the right cornor of the game. But can you please give an example on how to do this? Or send me the right wiki page? Because when I know how to make element data cash I can do much more. Link to comment
Moderators Citizen Posted March 6, 2011 Moderators Share Posted March 6, 2011 Maybe that ?: https://wiki.multitheftauto.com/wiki/ShowPlayerHudComponent Link to comment
proracer Posted March 6, 2011 Share Posted March 6, 2011 (edited) Ok you can try this but I'm not sure if it will work... function onLoginSetCash ( ) if not isGuestAccount ( getPlayerAccount ( source ) ) then if getElementData ( source, "data.cash" ) then outputChatBox ( "You're cash is transfered back!", source ) else setElementData ( source, "data.cash", 0 ) outputChatBox ( "You're new and your cash will now be saved." ) end end end function onWastedSetCash ( ) if not isGuestAccount ( getPlayerAccount ( source ) ) then local getPlayerCash = getElementData ( source, "data.cash" ) setElementData ( source, "data.cash", getPlayerCash + 100 ) outputChatBox ( "You got 100$ for your death.", source ) end end addEventHandler ( "onPlayerLogin", root, onLoginSetCash ) addEventHandler ( "onPlayerWasted", root, onWastedSetCash ) Edited March 6, 2011 by Guest Link to comment
bartje01 Posted March 6, 2011 Author Share Posted March 6, 2011 Nop. Even if I see the money thing. As proracer says: setPlayerMoney doesn't work for race. So can someone help me doing it with element data? Link to comment
proracer Posted March 6, 2011 Share Posted March 6, 2011 I have edited my last post, please try again. Link to comment
bartje01 Posted March 6, 2011 Author Share Posted March 6, 2011 Alright. It shows me the outputchatbox texts. But will this save? Even when I reconnect? And can someone help me making the cmd to check my cash? I tried: function myCash ( thePlayer, commandName ) local cash = getPlayerCash ( thePlayer ) outputChatBox ( "Your cash is: " .. cash, thePlayer ) end addCommandHandler ( "cash", myCash ) And failed. No message shows up when I type /cash Thank you very much Proracer Link to comment
12p Posted March 6, 2011 Share Posted March 6, 2011 function myCash ( thePlayer ) local cash = getPlayerMoney ( thePlayer ) outputChatBox ( "Your cash is: " .. tostring ( cash ), thePlayer ) end addCommandHandler ( "cash", myCash ) Link to comment
bartje01 Posted March 6, 2011 Author Share Posted March 6, 2011 Thanks for helping but, We're using element data cash now. You can see that in the posts above. I've edited it to this now function myCash ( thePlayer ) local cash = getPlayerCash ( thePlayer ) outputChatBox ( "Your cash is: " .. tostring ( cash ), thePlayer ) end addCommandHandler ( "cash", myCash ) Just no message shows up Link to comment
bamby12 Posted March 6, 2011 Share Posted March 6, 2011 (edited) sorry ignore my other post i didn't read it properly, if the script proracer put was right then i think to make the command would be: function myCash ( thePlayer ) local cash = getElementData ( thePlayer, "data.cash" ) --sorry my bad i put source instead of thePlayer outputChatBox ( "Your cash is: " .. tostring ( cash ), thePlayer ) end addCommandHandler ( "cash", myCash ) edited from The Kid, maybe im wrong. Edited March 6, 2011 by Guest Link to comment
proracer Posted March 6, 2011 Share Posted March 6, 2011 Yes, it will save when you even reconnect. There is also small alternative function if you wanna use: https://wiki.multitheftauto.com/wiki/SetAccountData Link to comment
bartje01 Posted March 6, 2011 Author Share Posted March 6, 2011 Yes it works all.Well almost When I type /cash it says: Your cash is: false LOl? Warning at this line local cash = getElementData ( source, "data.cash" ) Link to comment
Moderators Citizen Posted March 6, 2011 Moderators Share Posted March 6, 2011 Replace by this ?: function myCash ( thePlayer, commandName ) local cash = getElementData ( thePlayer, "data.cash" ) outputChatBox ( "Your cash is: " .. tostring( cash ), thePlayer ) end addCommandHandler ( "cash", myCash ) If you have this message again "Your cash is: false" it's because this elementData doesn't exist for this player Link to comment
bartje01 Posted March 6, 2011 Author Share Posted March 6, 2011 [2011-03-06 23:18:16] WARNING: testmode\server.lua:25: Bad argument @ 'getElementData' And it must be there. My whole script function onLoginSetCash ( ) if not isGuestAccount ( getPlayerAccount ( source ) ) then if getElementData ( source, "data.cash" ) then outputChatBox ( "You're cash is transfered back!", source ) else setElementData ( source, "data.cash", 0 ) outputChatBox ( "You're new and your cash will now be saved." ) end end end function onWastedSetCash ( ) if not isGuestAccount ( getPlayerAccount ( source ) ) then local getPlayerCash = getElementData ( source, "data.cash" ) setElementData ( source, "data.cash", getPlayerCash + 100 ) outputChatBox ( "You got 100$ for your death.", source ) end end addEventHandler ( "onPlayerLogin", root, onLoginSetCash ) addEventHandler ( "onPlayerWasted", root, onWastedSetCash ) function myCash ( thePlayer ) local cash = getElementData ( source, "data.cash" ) outputChatBox ( "Your cash is: " .. tostring ( cash ), thePlayer ) end addCommandHandler ( "cash", myCash ) Link to comment
Moderators Citizen Posted March 6, 2011 Moderators Share Posted March 6, 2011 Yeah try with this: function myCash ( thePlayer, commandName ) local cash = getElementData ( thePlayer, "data.cash" ) outputChatBox ( "Your cash is: " .. tostring( cash ), thePlayer ) end addCommandHandler ( "cash", myCash ) And tell me the result. Link to comment
Castillo Posted March 6, 2011 Share Posted March 6, 2011 Using element data to save stuff? when you reconnect the data is lost o,O. Link to comment
proracer Posted March 6, 2011 Share Posted March 6, 2011 How can the data be lost if you can use it on userpanel's and aswell on scoreboard data's ...? Link to comment
Castillo Posted March 6, 2011 Share Posted March 6, 2011 I save them in account data or sqlite, i don't remember well. 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