EngMohamedLish Posted June 6, 2015 Share Posted June 6, 2015 Hello guys, I would like to know how to save things i have in my server, like... Every script i make, its affects be saved when a player logout or quit. Like: Save player skin when he logout or quit, so when he come back, he find his skin didn't disappear Money as well, weapons, stats and everything? Also, I need to know the best way, and the easiest. Link to comment
EngMohamedLish Posted June 6, 2015 Author Share Posted June 6, 2015 setAccountData getAccountData Alright, I tried to make an example myself, but it doesn't work, could you check it? addEventHandler ("onPlayerQuit", root, function() local money = getPlayerMoney(source) setElementData (source, "Money", money) end) addEventHandler ("onPlayerLogin", root, function() getElementData(source, "Money") end) Link to comment
Noki Posted June 7, 2015 Share Posted June 7, 2015 You're using element data, which when assigned to a player, will reset when that player disconnects. So, you'll need to use account data. addEventHandler("onPlayerQuit", root, function () if (not isGuestAccount(source)) then local acc = getPlayerAccount(source) local money = getPlayerMoney(source) setAccountData(acc, "Money", money) end end ) addEventHandler("onPlayerLogin", root, function (_, theCurrentAccount) local money = getAccountData(theCurrentAccount, "Money") setPlayerMoney(source, tonumber(money)) end ) Using this eliminates the need for that element data, and instead you just fetch the money and save it when needed. Link to comment
EngMohamedLish Posted June 7, 2015 Author Share Posted June 7, 2015 You're using element data, which when assigned to a player, will reset when that player disconnects. So, you'll need to use account data. addEventHandler("onPlayerQuit", root, function () if (not isGuestAccount(source)) then local acc = getPlayerAccount(source) local money = getPlayerMoney(source) setAccountData(acc, "Money", money) end end ) addEventHandler("onPlayerLogin", root, function (_, theCurrentAccount) local money = getAccountData(theCurrentAccount, "Money") setPlayerMoney(source, tonumber(money)) end ) Using this eliminates the need for that element data, and instead you just fetch the money and save it when needed. Alright, I totally understand the script, although I don't understand this part, right here: function (_, theCurrentAccount) Could you explain it to me? about (_, theCurrentAccount) ? is theCurrentAccount is a function? Or just a name can be changed? and what is the next argument in that function (_, 'here') Link to comment
Walid Posted June 7, 2015 Share Posted June 7, 2015 theCurrentAccount it's an argument Read this : OnPlayerLogin event Link to comment
Noki Posted June 7, 2015 Share Posted June 7, 2015 Have a look at the onPlayerLogin event. You see the part where it says 'parameters'? They are basically the arguments that you can put in the functions that use that event. So, in our case, we only needed to know the current account, that was just logged into using onPlayerLogin. So, we used an underscore to nullify the other argument (thePreviousAccount), and proceeded to write theCurrentAccount as our second argument. By doing this, we're bypassing the first paramater, because we don't need it. The wiki page for that event should show any possible parameters you're able to use with this function. Link to comment
EngMohamedLish Posted June 7, 2015 Author Share Posted June 7, 2015 theCurrentAccount it's an argument Read this : OnPlayerLogin event Alright, Understood, but why do you place _, while don't you do function (theCurrentAccount) instead of (_, theCurrentAccount) Also, I do not understand what: thePreviousAccount: The account the player was logged into before thePreviousAccount mention to what? ... an account that played logged Into before? I didn't get it? could you tell me more ? Link to comment
EngMohamedLish Posted June 7, 2015 Author Share Posted June 7, 2015 Have a look at the onPlayerLogin event. You see the part where it says 'parameters'? They are basically the arguments that you can put in the functions that use that event. So, in our case, we only needed to know the current account, that was just logged into using onPlayerLogin. So, we used an underscore to nullify the other argument (thePreviousAccount), and proceeded to write theCurrentAccount as our second argument. By doing this, we're bypassing the first paramater, because we don't need it. The wiki page for that event should show any possible parameters you're able to use with this function. OHH! so we pass "thePreviousAccount" by "_," Right?! Link to comment
Noki Posted June 7, 2015 Share Posted June 7, 2015 Have a look at the onPlayerLogin event. You see the part where it says 'parameters'? They are basically the arguments that you can put in the functions that use that event. So, in our case, we only needed to know the current account, that was just logged into using onPlayerLogin. So, we used an underscore to nullify the other argument (thePreviousAccount), and proceeded to write theCurrentAccount as our second argument. By doing this, we're bypassing the first paramater, because we don't need it. The wiki page for that event should show any possible parameters you're able to use with this function. OHH! so we pass "thePreviousAccount" by "_," Right?! I basically explained everything that Walid didn't. Anyway, Yes, we are using '_' because we do not need to pass thePreviousAccount. However, that is only for this case. Don't just think that parameter is not needed at all, because it can be useful from time to time. Only underscore if you do not require a parameter that is before one you want to use. You can also rename the parameters, as they only rely on chronological order. For example, I could call thePreviousAccount 'TPA' if I wanted to. It would work the same way. But, the next one would be theCurrentAccount, I can call it whatever I want. Link to comment
EngMohamedLish Posted June 7, 2015 Author Share Posted June 7, 2015 Have a look at the onPlayerLogin event. You see the part where it says 'parameters'? They are basically the arguments that you can put in the functions that use that event. So, in our case, we only needed to know the current account, that was just logged into using onPlayerLogin. So, we used an underscore to nullify the other argument (thePreviousAccount), and proceeded to write theCurrentAccount as our second argument. By doing this, we're bypassing the first paramater, because we don't need it. The wiki page for that event should show any possible parameters you're able to use with this function. OHH! so we pass "thePreviousAccount" by "_," Right?! I basically explained everything that Walid didn't. Anyway, Yes, we are using '_' because we do not need to pass thePreviousAccount. However, that is only for this case. Don't just think that parameter is not needed at all, because it can be useful from time to time. Only underscore if you do not require a parameter that is before one you want to use. You can also rename the parameters, as they only rely on chronological order. For example, I could call thePreviousAccount 'TPA' if I wanted to. It would work the same way. But, the next one would be theCurrentAccount, I can call it whatever I want. Thank you very much, I understood everything. Whatever, I just made an example myself, but i didn't get any bug through /debugscript 3, whatever, It doesn't work. Could you check it, and tell me what's the problem with it? addEventHandler("onPlayerQuit", root, function(source) if isGuestAccount(source) then return end local account = getPlayerAccount(source) local money = getPlayerMoney(source) setAccountData(account, "Money", money) end) addEventHandler("onPlayerLogin", root, function(_, theCurrentAccount) getAccountData (theCurrentAccount, "Money") end) Link to comment
Walid Posted June 7, 2015 Share Posted June 7, 2015 it should be like this addEventHandler("onPlayerQuit", root, function() local account = getPlayerAccount(source) if isGuestAccount(account) then return end local money = getPlayerMoney(source) setAccountData(account, "Money", money) end ) addEventHandler("onPlayerLogin", root, function(_, theCurrentAccount) local money = getAccountData (theCurrentAccount, "Money") if money then setPlayerMoney(source, tonumber(money)) end end) Link to comment
EngMohamedLish Posted June 7, 2015 Author Share Posted June 7, 2015 it should be like this addEventHandler("onPlayerQuit", root, function() local account = getPlayerAccount(source) if isGuestAccount(account) then return end local money = getPlayerMoney(source) setAccountData(account, "Money", money) end ) addEventHandler("onPlayerLogin", root, function(_, theCurrentAccount) local money = getAccountData (theCurrentAccount, "Money") if money then setPlayerMoney(source, tonumber(money)) end end) Alright, thanks. And actually, i see something that's not going to be easy, to be made. I tried to save the player position, and when he joins he spawns there. but doesn't work: no errors as well addEventHandler ("onPlayerQuit", root, function () if isGuestAccount(source) then return end x, y, z = getElementPosition(source) acc = getPlayerAccount(source) setAccountData (acc, "x.s", x) setAccountData (acc, "y.s", y) setAccountData (acc, "z.s", z) end) addEventHandler ("onPlayerLogin", root, function(_, account) ax = getAccountData (account, "x.s") ay = getAccountData (account, "x.s") az = getAccountData (account, "x.s") spawnPlayer(source, ax, ay, az) end) Link to comment
EngMohamedLish Posted June 7, 2015 Author Share Posted June 7, 2015 it should be like this addEventHandler("onPlayerQuit", root, function() local account = getPlayerAccount(source) if isGuestAccount(account) then return end local money = getPlayerMoney(source) setAccountData(account, "Money", money) end ) addEventHandler("onPlayerLogin", root, function(_, theCurrentAccount) local money = getAccountData (theCurrentAccount, "Money") if money then setPlayerMoney(source, tonumber(money)) end end) Alright, thanks. And actually, i see something that's not going to be easy, to be made. I tried to save the player position, and when he joins he spawns there. but doesn't work: no errors as well addEventHandler ("onPlayerQuit", root, function () if isGuestAccount(source) then return end x, y, z = getElementPosition(source) acc = getPlayerAccount(source) setAccountData (acc, "x.s", x) setAccountData (acc, "y.s", y) setAccountData (acc, "z.s", z) end) addEventHandler ("onPlayerLogin", root, function(_, account) ax = getAccountData (account, "x.s") ay = getAccountData (account, "x.s") az = getAccountData (account, "x.s") spawnPlayer(source, ax, ay, az) end) Actually, solved it Thanks for your help, anyways ! Link to comment
Walid Posted June 7, 2015 Share Posted June 7, 2015 It can be like this addEventHandler ("onPlayerQuit", root, function () local account = getPlayerAccount(source) if account and not isGuestAccount(source) then local x, y, z = getElementPosition(source) setAccountData (account, "position", toJSON({x,y,z})) end end ) addEventHandler ("onPlayerLogin", root, function(_, account) local position = getAccountData(account,"position") if postion then local x, y, z = unpack(fromJSON(position)) spawnPlayer(source, x, y, z) end ) Link to comment
EngMohamedLish Posted June 7, 2015 Author Share Posted June 7, 2015 It can be like this addEventHandler ("onPlayerQuit", root, function () local account = getPlayerAccount(source) if account and not isGuestAccount(source) then local x, y, z = getElementPosition(source) setAccountData (account, "position", toJSON({x,y,z})) end end ) addEventHandler ("onPlayerLogin", root, function(_, account) local position = getAccountData(account,"position") if postion then local x, y, z = unpack(fromJSON(position)) spawnPlayer(source, x, y, z) end ) Aha, Thank you man It is looking more professional idea, than the one i made ! I just don't understand something here, It is my first time to see "toJSON" and fromJSON" what is that, actually? Could you explain it, for me ? THANKS ! ! ! Link to comment
Noki Posted June 7, 2015 Share Posted June 7, 2015 JSON converts tables into variables and strings into variables. Line 6 has x, y and z in a table, and is saving that table as a string. When you login, it converts that string into a variable. Link to comment
EngMohamedLish Posted June 7, 2015 Author Share Posted June 7, 2015 JSON converts tables into variables and strings into variables. Line 6 has x, y and z in a table, and is saving that table as a string. When you login, it converts that string into a variable. Thanks ! And, what a smart function you use ! 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