kevin11 Posted May 19, 2010 Share Posted May 19, 2010 (edited) hey a friend made a color script for youre name, how can i save it into account.xml? with setaccountdata blabla function nameTagColor2 ( redT,greenT,blueT, r, g, b ) setPlayerNametagColor ( source, redT,greenT,blueT ) end addEvent( "nameTag", true ) addEventHandler ( "nameTag", getRootElement(), nameTagColor2) Edited May 19, 2010 by Guest Link to comment
dzek (varez) Posted May 19, 2010 Share Posted May 19, 2010 without the script we cannot help you.. we should know where and how it saved etc Link to comment
kevin11 Posted May 20, 2010 Author Share Posted May 20, 2010 (edited) i tried some things but still doesnt really work:( anyone knows how? dont laugh but im bad in this here is what i did function onPlayerQuit ( ) local playeraccount = getPlayerAccount ( source ) if ( playeraccount ) then local nametagColor = getPlayerNametagColor ( source ) setAccountData ( playeraccount, "nameColor", nametagColor ) end end function onPlayerLogin ( ) local playeraccount = getPlayerAccount ( source ) if ( playeraccount ) then local nametagColor = getAccountData ( playeraccount, "nameColor" ) if ( nametagColor ) then setPlayerNametagColor ( thePlayer, r, g, b ) end end end function onPlayerSpawn ( ) local playeraccount = getPlayerAccount ( source ) if ( playeraccount ) then local nametagColor = getAccountData ( playeraccount, "nameColor" ) if ( nametagColor ) then setPlayerNametagColor ( thePlayer, r, g, b ) end end end addEventHandler ( "onPlayerQuit", getRootElement ( ), onPlayerQuit ) addEventHandler ( "onPlayerLogin", getRootElement ( ), onPlayerLogin ) addEventHandler ( "onPlayerSpawn", getRootElement ( ), onPlayerSpawn ) Edited May 21, 2010 by Guest Link to comment
50p Posted May 20, 2010 Share Posted May 20, 2010 anyone? Yes, you. Do you think colour is just 1 value? It is for drawing functions (client-side) but not for name. Why don't you try to think for a second or two? Look at setPlayerNametagColor function parameters at wiki and ask yourself: "What should I save that I can load/use later in setPlayerNametagColor?" I'm sure you can answer this question within time of 1 second after seeing setPlayerNametagColor parameters. Link to comment
kevin11 Posted May 21, 2010 Author Share Posted May 21, 2010 anyone? Yes, you. Do you think colour is just 1 value? It is for drawing functions (client-side) but not for name. Why don't you try to think for a second or two? Look at setPlayerNametagColor function parameters at wiki and ask yourself: "What should I save that I can load/use later in setPlayerNametagColor?" I'm sure you can answer this question within time of 1 second after seeing setPlayerNametagColor parameters. k u can also try saying it at a normal way im just asking Link to comment
50p Posted May 21, 2010 Share Posted May 21, 2010 This is so obvious that I'm sure you wouldn't have to make a new thread to ask this question. Do you still wonder how to save it? I'll be willing to write these few lines for you if you're still having problems. Link to comment
kevin11 Posted May 21, 2010 Author Share Posted May 21, 2010 This is so obvious that I'm sure you wouldn't have to make a new thread to ask this question. Do you still wonder how to save it? I'll be willing to write these few lines for you if you're still having problems. ok ty (im trying to learn lua ) Link to comment
Antibird Posted May 21, 2010 Share Posted May 21, 2010 (edited) function onPlayerQuit ( ) local playeraccount = getPlayerAccount ( source ) if ( playeraccount ) then local nametagColor = getPlayerNametagColor ( source ) --INCORRECT! Delete it! --It only saves the RED color value in "nametagColor" variable, skipping Green and Blue one, so: local r, g, b = getPlayerNametagColor ( source ) -- is correct --Then, you can't save just 3 variables as a single account data, it requires a workaround: local nametagColor = { r, g, b } --we put everything in a table { }.. local dataToSave = toJSON( nametagColor ) -- .. and then transform the table using [url=https://wiki.multitheftauto.com/wiki/ToJSON]toJSON[/url] function. -- now we've got a table of colors in a string form so we can.. setAccountData ( playeraccount, "nameColor", dataToSave ) -- ..save it eventually end end function onPlayerLogin ( ) local playeraccount = getPlayerAccount ( source ) if ( playeraccount ) then local nametagColor = getAccountData ( playeraccount, "nameColor" ) if ( nametagColor ) then setPlayerNametagColor ( thePlayer, r, g, b ) --INCORRECT! Remove the line! --Look, you don't have "r", "g", "b" declared yet, you only have "nametagColor" one so we make a reverse transformation: .. local colorTable = fromJSON( nametagColor ) --..put out string back into table form using [url=https://wiki.multitheftauto.com/wiki/FromJSON]fromJSON[/url] function.. local r, g, b = unpack( colorTable ) --.. and get all 3 values from it with [url=http://www.lua.org/manual/5.1/manual.html#pdf-unpack]unpack[/url] function -- so now we've got RGB values for using in setPlayerNametagColor() setPlayerNametagColor ( thePlayer, r, g, b ) -- Now it's correct. end end end ( same for onPlayerSpawn() ) Of course this code can be shortened, but you must understand what each line does before trying to optimize. P.S. Yep, sorry. A mistype at line 23. Edited May 21, 2010 by Guest Link to comment
kevin11 Posted May 21, 2010 Author Share Posted May 21, 2010 50p can u fix it for me ur pro function onPlayerQuit ( ) local playeraccount = getPlayerAccount ( source ) if ( playeraccount ) then local r, g, b = getPlayerNametagColor ( source ) local nametagColor = { r, g, b } local dataToSave = toJSON( nametagColor ) setAccountData ( playeraccount, "nameColor", dataToSave ) end end function onPlayerLogin ( ) local playeraccount = getPlayerAccount ( source ) if ( playeraccount ) then local nametagColor = getAccountData ( playeraccount, "nameColor" ) if ( nametagColor ) then local colorTable = fromJSON( nametagColor ) local r, g, b = unpack( fromJSON ) setPlayerNametagColor ( thePlayer, r, g, b ) end end end addEventHandler ( "onPlayerQuit", getRootElement ( ), onPlayerQuit ) addEventHandler ( "onPlayerLogin", getRootElement ( ), onPlayerLogin ) Link to comment
Dark Dragon Posted May 21, 2010 Share Posted May 21, 2010 at line 17 you should unpack colorTable i'd say. Link to comment
50p Posted May 21, 2010 Share Posted May 21, 2010 I've been having problems with toJSON and fromJSON (even with JSON data in C#, that's why my SDK doesn't have any classes to help with returned data from MTA server). Just save every channel separately or: Where is thePlayer variable coming from? Antibrid, how can you unpack a function (tip: fromJSON, line 23)? Also, why do you create 3 separate variables which are used once (go straight to table)? Why not simply: local nameColor = { getPlayerNametagColor( source ) }; Link to comment
Antibird Posted May 21, 2010 Share Posted May 21, 2010 at line 17 you should unpack colorTable i'd say. My fail, fixed in code above. He just copied and stuck with that error. @50p: everything comes of the script kevin11 posted, thePlayer as well. I just tried to explain the errors he had and show a way to avoid. Using 3 variables is only for visibility purposes. Of course this code can be shortened, but you must understand what each line does before trying to optimize. Link to comment
50p Posted May 21, 2010 Share Posted May 21, 2010 at line 17 you should unpack colorTable i'd say. My fail, fixed in code above. He just copied and stuck with that error. @50p: everything comes of the script kevin11 posted, thePlayer as well. I just tried to explain the errors he had and show a way to avoid. Using 3 variables is only for visibility purposes. Of course this code can be shortened, but you must understand what each line does before trying to optimize. Fair enough but I didn't ask you about thePlayer. If you're fixing code, fix it so it works or don't fix it and give tips so that author can try to fix it himself. When you're giving sample code, don't copy & paste original code but show the sample code only. That way he learns and tries to implement the code in his script. I know many people don't like my way of teaching but after all they're thankful and actually learn something, not just getting the code, disappearing from the forum and coming back for more code when in need. Anyway, let's just wait for topic's author to reply. Link to comment
kevin11 Posted May 21, 2010 Author Share Posted May 21, 2010 can anyone fix my original code, so it will work? Link to comment
robhol Posted May 21, 2010 Share Posted May 21, 2010 My faith in keving11 is gone You had faith in kevin11? Link to comment
kevin11 Posted May 21, 2010 Author Share Posted May 21, 2010 is this better???? can anyone just fix it so i can see what i did wrong:S function onPlayerQuit ( ) local playeraccount = getPlayerAccount ( source ) if ( playeraccount ) then local nametagColor = getPlayerNametagColor ( source ) setPlayerNametagColor ( thePlayer, "r" ) setPlayerNametagColor ( thePlayer, "g" ) setPlayerNametagColor ( thePlayer, "b" ) end end function onPlayerLogin ( ) local playeraccount = getPlayerAccount ( source ) if ( playeraccount ) then local nametagColor = getAccountData ( playeraccount, "nameColor" ) if ( nametagColor ) then setPlayerNametagColor ( thePlayer, "r" ) setPlayerNametagColor ( thePlayer, "g" ) setPlayerNametagColor ( thePlayer, "b" ) end end end function onPlayerSpawn ( ) local playeraccount = getPlayerAccount ( source ) if ( playeraccount ) then local nametagColor = getAccountData ( playeraccount, "nameColor" ) if ( nametagColor ) then setPlayerNametagColor ( thePlayer, "r" ) setPlayerNametagColor ( thePlayer, "g" ) setPlayerNametagColor ( thePlayer, "b" ) end end end addEventHandler ( "onPlayerQuit", getRootElement ( ), onPlayerQuit ) addEventHandler ( "onPlayerLogin", getRootElement ( ), onPlayerLogin ) addEventHandler ( "onPlayerSpawn", getRootElement ( ), onPlayerSpawn ) Link to comment
Castillo Posted May 21, 2010 Share Posted May 21, 2010 ok well, i checked all those codes and edited it by myself and works it, saves the player tag color and when login it loads the saved one. here is code function onPlayerQuit ( ) local playeraccount = getPlayerAccount ( source ) if ( playeraccount ) then local r, g, b = getPlayerNametagColor ( source ) setAccountData ( playeraccount, "ColorRed", r ) setAccountData ( playeraccount, "ColorGreen", g ) setAccountData ( playeraccount, "ColorBlue", b ) end end function onPlayerLogin ( ) local playeraccount = getPlayerAccount ( source ) if ( playeraccount ) then local tagColorRed = getAccountData ( playeraccount, "ColorRed" ) local tagColorGreen = getAccountData ( playeraccount, "ColorGreen" ) local tagColorBlue = getAccountData ( playeraccount, "ColorBlue" ) if tagColorRed and tagColorGreen and tagColorBlue then setPlayerNametagColor ( source, tagColorRed, tagColorGreen, tagColorBlue ) end end end addEventHandler ( "onPlayerQuit", getRootElement ( ), onPlayerQuit ) addEventHandler ( "onPlayerLogin", getRootElement ( ), onPlayerLogin ) Greetings Castillo 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