Dzsozi (h03) Posted December 15, 2014 Share Posted December 15, 2014 Hello community, I have a question/problem with my script. So, I don't really understand how does the loops work, I mean these things: for k, v, in ipairs(somethingHere) do -- or for i, somethingHere in #numberOfSomething do -- and more 'for ... do' things First of all I would be very grateful if someone could explain it or send me a source to read through and learn it. So my problem is that I took the wiki's example and edited it, but for some reason it's not working. Since I don't understand loops I can't manage out how to fix it, even if debugscript tells me errors. I want to loop through all the accounts and set their "funmodev2-" account datas to false, because I don't need them anymore. Here's my code: function deleteAllData ( ) local account = getPlayerAccount (source) local accountTable = getAccounts () for accountTable, accounts in ipairs(getAccountName(account)) do local data = getAllAccountData( accounts ) for k,v in pairs ( data ) do setAccountData (v, "funmodev2-money", false) setAccountData (v, "funmodev2-skin", false) setAccountData (v, "funmodev2-health", false) setAccountData (v, "funmodev2-armor", false) setAccountData (v, "funmodev2-team", false) setAccountData (v, "funmodev2-R", false) setAccountData (v, "funmodev2-G", false) setAccountData (v, "funmodev2-B", false) setAccountData (v, "funmodev2-x", false) setAccountData (v, "funmodev2-y", false) setAccountData (v, "funmodev2-z", false) setAccountData (v, "funmodev2-int", false) setAccountData (v, "funmodev2-dim", false) setAccountData (v, "funmodev2-wantedlevel", false) setAccountData (v, "funmodev2-weaponID0", false) setAccountData (v, "funmodev2-weaponID1", false) setAccountData (v, "funmodev2-weaponID2", false) setAccountData (v, "funmodev2-weaponID3", false) setAccountData (v, "funmodev2-weaponID4", false) setAccountData (v, "funmodev2-weaponID5", false) setAccountData (v, "funmodev2-weaponID6", false) setAccountData (v, "funmodev2-weaponID7", false) setAccountData (v, "funmodev2-weaponID8", false) setAccountData (v, "funmodev2-weaponID9", false) setAccountData (v, "funmodev2-weaponID10", false) setAccountData (v, "funmodev2-weaponID11", false) setAccountData (v, "funmodev2-weaponID12", false) setAccountData (v, "funmodev2-weaponAmmo0", false) setAccountData (v, "funmodev2-weaponAmmo1", false) setAccountData (v, "funmodev2-weaponAmmo2", false) setAccountData (v, "funmodev2-weaponAmmo3", false) setAccountData (v, "funmodev2-weaponAmmo4", false) setAccountData (v, "funmodev2-weaponAmmo5", false) setAccountData (v, "funmodev2-weaponAmmo6", false) setAccountData (v, "funmodev2-weaponAmmo7", false) setAccountData (v, "funmodev2-weaponAmmo8", false) setAccountData (v, "funmodev2-weaponAmmo9", false) setAccountData (v, "funmodev2-weaponAmmo10", false) setAccountData (v, "funmodev2-weaponAmmo11", false) setAccountData (v, "funmodev2-weaponAmmo12", false) end end end addCommandHandler( "delallacc", deleteAllData ) When I write /delallacc debugscript tells me this: Thanks for the help in advance! P.S.: Sorry for the bad English if there was something written wrong, I'm from Hungary! Link to comment
johny46 Posted December 15, 2014 Share Posted December 15, 2014 Both of the loops you mentioned in your post are used to iterate through all (not necessarily) elements of a table. Let's say you've got a table like this: myTable = {5, 4, 3, 2, 1} If you wanted to execute some specific code on each element of the table, you would have to iterate through it. It can be done in a lot of ways, but let's first take the one easiest to understand: for index = 1, #myTable, 1 do outputChatBox(myTable[index]) end What this code is, is basically a regular "for" loop, where the number of repetitions is determined by the length of the table. In this case, #myTable returns 5, so the code inside is executed 5 times, then it continues on something else. With each loop, the index variable gets increased by one, so you can then do myTable[index] in order to work with the values of the table. So with each loop you can output one value of the table, or do any other stuff with it. Now onto something slightly different, a for loop with an iterator: for index, value in ipairs(myTable) do outputChatBox(value) end This code should output exactly the same data as the earlier loop, but it uses different construct. What this loop does, is automatically going through the entire table and with each repetition, it updates the index and value variables with current ones. The value variable is going to be the same as myTable[index], but it is just more convenient because you don't have to read these values manually. For further reading and some more examples I recommend you to go to http://www.phailed.me/2011/02/learn-lua ... ay-tables/ It explains the whole thing really great. If you think you've understand everything I was talking about, then it will be really easy for you to spot the obvious misconceptions and errors in your code, so you will be able to fix it yourself. Link to comment
Dzsozi (h03) Posted December 15, 2014 Author Share Posted December 15, 2014 Thanks! You helped me alot! I'll read through the page you have linked, thanks again! Link to comment
Dzsozi (h03) Posted December 15, 2014 Author Share Posted December 15, 2014 Okay, so now I can get the number of the accounts in the server with this code right? It outputs 4. One is mine, other one is my friend's, the other 2 are test accounts I think. But now I want to get all the accounts name and set their account data to false. But the getAccounts function only returns to numbers. How can I get all the account's names? To set their data and delete them. Here's my current code, as I've mentioned it outputs the number of the accounts right now: function deleteAllData ( ) local accountTable = getAccounts () for i=1, #accountTable, 1 do outputChatBox(i) end end addCommandHandler( "delallacc", deleteAllData ) Link to comment
tosfera Posted December 15, 2014 Share Posted December 15, 2014 First of all, you don't need that kind of loop in this case because you can do it easier. Sure, there is a way how to do it that way and it's at the bottom of this post. We'll take a look at the 'in ipairs' loop. I'll also explain what it's actually doing, just in case; for i, account in ipairs ( getAccounts() ) do outputChatBox ( getAccountName ( account ) ) end There are two variables in this one; i and account: I is the index of the loop, this can bse useful for everything but you can also blank it out now because you aren't using it. ( Blank it out with a '_' like: for _, account in ) account is the actual account from the table getAccounts(), you can use this for everything. If you still want to use the for i = 0, #getAccounts do then you should do this; local accounts = getAccounts() for i = 0, #accounts do outputChatBox ( getAccountName ( accounts [ i ] ) ); end 'accounts' is a table holding all the account elements, you have to get them out at the specific location by using the index ( i ). Link to comment
Dzsozi (h03) Posted December 15, 2014 Author Share Posted December 15, 2014 Thank you! I've managed out how to do this, and it's working! Thanks everybody again! 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