Einheit-101 Posted February 23, 2014 Share Posted February 23, 2014 (edited) Hello community. I created a little rank system and i have a big code with a lot of unnecessary lines which could be done with some simple lines in a "for" loop. My question is, does the following code the same thing as the second following code? First Code: local usranks { "[1]PVT","[2]PFC","[3]CPL","[4]SGT","[5]SSG","[6]SFC","[7]MSG","[8]1SG","[9]SGM","[10]CSM","[11]SMA"},"[12]2LT","[13]1LT","[14]CPT","[15]MAJ","[16]LTC","[17]COL","[18]BG","[19]MG","[20]LTG","[21]GEN","[22]GA" } function playerSpawn ( ) playersource = source for index, usRank in ipairs(usranks) do if (usRank == usranks[2]) then triggerClientEvent("r1", playersource) elseif (usRank == usranks[2]) then triggerClientEvent("r2", playersource) elseif (usRank == usranks[3]) then triggerClientEvent("r3", playersource) elseif (usRank == usranks[3]) then triggerClientEvent("r4", playersource) elseif (usRank == usranks[3]) then triggerClientEvent("r5", playersource) elseif (usRank == usranks[3]) then triggerClientEvent("r6", playersource) elseif (usRank == usranks[3]) then triggerClientEvent("r7", playersource) elseif (usRank == usranks[3]) then triggerClientEvent("r8", playersource) elseif (usRank == usranks[3]) then triggerClientEvent("r9", playersource) elseif (usRank == usranks[3]) then triggerClientEvent("r10", playersource) elseif (usRank == usranks[3]) then triggerClientEvent("r11", playersource) elseif (usRank == usranks[3]) then triggerClientEvent("r12", playersource) elseif (usRank == usranks[3]) then triggerClientEvent("r13", playersource) elseif (usRank == usranks[3]) then triggerClientEvent("r14", playersource) elseif (usRank == usranks[3]) then triggerClientEvent("r15", playersource) elseif (usRank == usranks[3]) then triggerClientEvent("r16", playersource) elseif (usRank == usranks[3]) then triggerClientEvent("r17", playersource) elseif (usRank == usranks[3]) then triggerClientEvent("r18", playersource) elseif (usRank == usranks[3]) then triggerClientEvent("r19", playersource) elseif (usRank == usranks[3]) then triggerClientEvent("r20", playersource) elseif (usRank == usranks[3]) then triggerClientEvent("r21", playersource) elseif (usRank == usranks[3]) then triggerClientEvent("r22", playersource) end end end addEventHandler("onPlayerSpawn", getRootElement(), playerSpawn) Second Code: local usranks { "[1]PVT","[2]PFC","[3]CPL","[4]SGT","[5]SSG","[6]SFC","[7]MSG","[8]1SG","[9]SGM","[10]CSM","[11]SMA"},"[12]2LT","[13]1LT","[14]CPT","[15]MAJ","[16]LTC","[17]COL","[18]BG","[19]MG","[20]LTG","[21]GEN","[22]GA" } function playerSpawn ( ) playersource = source for index, therank in ipairs(usranks) do for i = 1, 22 do if (therank == usranks[i]) then triggerClientEvent("r"..tostring(i), playersource) end end end end addEventHandler("onPlayerSpawn", getRootElement(), playerSpawn) Thanks in forward. Edited February 23, 2014 by Guest Link to comment
Wei Posted February 23, 2014 Share Posted February 23, 2014 Yes, they are basicaly the same Link to comment
Einheit-101 Posted February 23, 2014 Author Share Posted February 23, 2014 Thank you. I will now try to change all the code and report if it worked for me. Link to comment
Einheit-101 Posted February 23, 2014 Author Share Posted February 23, 2014 It seems to work a little bit, but i get the following error: This code should compare the current Rank with the ones in the table and trigger the Client event with the correct rank. local usranks = { {"[1]PVT"},{"[2]PFC"},{"[3]CPL"},{"[4]SGT"},{"[5]SSG"},{"[6]SFC"},{"[7]MSG"},{"[8]1SG"},{"[9]SGM"},{"[10]CSM"},{"[11]SMA"},{"[12]2LT"},{"[13]1LT"},{"[14]CPT"},{"[15]MAJ"},{"[16]LTC"},{"[17]COL"},{"[18]BG"},{"[19]MG"},{"[20]LTG"},{"[21]GEN"},{"[22]GA"} } for i, therank in ipairs(usranks) do outputChatBox(""..tostring(therank)) outputChatBox(""..tostring(usRank)) if (therank == usRank) then --does not work because therank is not a String triggerClientEvent("r"..tostring(i), playersource) end end Now the first outputChatBox returns a userdata value like "0507E8E" and the second outputs the correct Rank of the Player: "[1]PVT". Why does the variable "therank" return a userdata value and not the String of the table above? I can not compare Userdata values and Strings... €DIT::: I got it. I have to remove the brackets in the Table: local usranks = { {"[1]PVT"}, ... -- change to: local usranks = { "[1]PVT", ... Link to comment
Moderators Citizen Posted February 23, 2014 Moderators Share Posted February 23, 2014 I was exactly writing that your code didn't make any sense since it was triggering ALL the ranks ! And not the current player rank. You first need to get the current player rank (i dunno where you are saving that data, but i would suggest an element data). Once you got the rank (the string version) then you loop the table usranks (in your loop, therank will hold the rank he gets from the table. So at 1st loop, therank will be "[1]PVT" then for the 2nd loop, it will be "[2]PFC" and so on.) then your condition inside the loop will be: if playerRank == therank then --where playerRank is the current rank of the player you got from getElementData and not: if therank == usranks[i] then because they both are the same because the loop is doing usranks to set the value of therank. Hope it's more clear. Link to comment
Einheit-101 Posted February 24, 2014 Author Share Posted February 24, 2014 thank you, i got it working some hours ago with some experimenting indeed, 2 for loops are useless and i got 22x all ranks returned in the chatbox, this way i found out how to do it correct 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