.:HyPeX:. Posted October 24, 2013 Share Posted October 24, 2013 Hello guys, is there a way i can find a specific player by checking a key value of account data? lets stay i stored a player's name under the key "name": setAccountData(player, "name", getPlayerName( player)) Now, how could i find it? becouse supposing i use the key value to use longer nicknames on players, and i need to find one by its nick, i would then need to do a miracle to match a players nickname and its real nick. Thanks HyPeX Link to comment
csiguusz Posted October 24, 2013 Share Posted October 24, 2013 You can get all the accounts with getAccounts, then loop through this table, get their "name" account data(getAccountData) and check if it is the one wich you want to find. But I don't recommend doing this very often, especially if you have many acconts. Link to comment
.:HyPeX:. Posted October 24, 2013 Author Share Posted October 24, 2013 Yeah, thought of this already, becouse i'm using it as a script for letting players have longer nicknames, but it isnt easy, since we have this problem.. and thats why i was asking if there was a better method. PD: could you help me out with the code to loop them? i'm not good doing loops at all Link to comment
csiguusz Posted October 24, 2013 Share Posted October 24, 2013 There's an example of the loop: local accounts = getAccounts () for i, v in ipairs ( accounts ) do local name = getAccountData ( v, "name" ) -- do other things here... end Edit: replaced getElementData with getAccount data... sorry for the wrong function Link to comment
.:HyPeX:. Posted October 24, 2013 Author Share Posted October 24, 2013 Thanks, i'll test it and tell how it goes on the nick getting. PD: just wondering, could i do it with the actual players in the server? to make it easier for making a table with them. Thanks HyPeX EDIT: i'm not sure how to find a data value with the function you gave me, could you help me out? EDIT2: Something like this maybe? function getNicks(player, command, nick) local accounts = getAccounts () for i, v in ipairs ( accounts ) do local named = getAccountData ( v, "name" ) local nameacc = getAccountPlayer( v ) local name = getPlayerName( nameacc ) if string.find(named, nick) then outputChatBox("[Nick]: Player: ".. named ..", real nick: ".. name ..", account: ".. v .."", source, 0,175,255, false) end end end addCommandHandler("getNick", getNicks) Link to comment
csiguusz Posted October 24, 2013 Share Posted October 24, 2013 PD: just wondering, could i do it with the actual players in the server? to make it easier for making a table with them. It's a good idea, and possibly the best way to do what you want. Store the player elements and their "real names" in a table and save it only when the resource stops. ( or even save it eg. every 10 minutes with a timer if you want to be sure that nothing will get lost ). Searching in that table would be easier. Your code looks good. It should work, I've just notived one problem: you can't output the "v" variable. use tostring(v) or getAccountName(v). Link to comment
.:HyPeX:. Posted October 24, 2013 Author Share Posted October 24, 2013 Just left aside the table idea, i dont know how to use the concat correctly or finding an element in the whole table. nothing happens when i do "/getNick hypex" (my nickname is hypex) function getNicks(player, command, nick) local accounts = getAccounts () for i, v in ipairs ( accounts ) do local named = getAccountData ( v, "name" ) if getAccountPlayer( tostring(v) ) ~= false then if string.match(named, nick) then local nameacc = getAccountPlayer( tostring(v) ) local name = getPlayerName( nameacc ) outputChatBox("[Nick]: Player: ".. named ..", real nick: ".. name ..", account: ".. v .."", source, 0,175,255, false) else outputChatBox("[Nick]: Account: ".. tostring(v) .." not logged in", source, 0,175,255, false) end else outputChatBox("[Nick]: failed to find a match!", source, 0,175,255, false) end end end addCommandHandler("getNick", getNicks) Link to comment
csiguusz Posted October 24, 2013 Share Posted October 24, 2013 Always use "/debugscript 3" while testing, it would have told you the errors. You are using the tostring function wrong. It can make a string from anything so you will be able to output it as a text. But where you need an element, don't use tostring(v). (v is the account element) Link to comment
.:HyPeX:. Posted October 24, 2013 Author Share Posted October 24, 2013 But its is getting an error if i dont use tostring.. Link to comment
.:HyPeX:. Posted October 24, 2013 Author Share Posted October 24, 2013 What kind of error? nvm i was trying only the command itself without specify. added this if not nick then outputChatBox("[Nick]: Please specify a nick!", source, 0,175,255, false) return end fortunately i dont have many accounts in my local server.. this happened when i tried to do /getnick hypex.. it didnt returned me.. local accounts = getAccounts () for i, v in ipairs ( accounts ) do local named = getAccountData ( v, "name" ) if named then if string.match("named", nick) then local nameacc = getAccountPlayer(v) local name = getPlayerName( tostring(nameacc) ) outputChatBox("[Nick]: Player: ".. named ..", real nick: ".. name ..", account: ".. v .."", source, 0,175,255, false) else outputChatBox("[Nick]: Account: ".. tostring(v) .." not logged in", source, 0,175,255, false) end else outputChatBox("[Nick]: this acc has no named!", source, 0,175,255, false) end end end addCommandHandler("getNick", getNicks) [Nick]: Please specify a nick! [Nick]: Account: userdata: 02000126 not logged in [Nick]: this acc has no named! [Nick]: this acc has no named! [Nick]: this acc has no named! [Nick]: this acc has no named! [Nick]: this acc has no named! [Nick]: this acc has no named! [Nick]: this acc has no named! [Nick]: this acc has no named! [Nick]: this acc has no named! [Nick]: this acc has no named! [Nick]: this acc has no named! [Nick]: this acc has no named! [Nick]: this acc has no named! [Nick]: this acc has no named! [Nick]: this acc has no named! [Nick]: this acc has no named! [Nick]: this acc has no named! [Nick]: this acc has no named! [Nick]: this acc has no named! [Nick]: this acc has no named! [Nick]: this acc has no named! [Nick]: this acc has no named! [Nick]: this acc has no named! [Nick]: this acc has no named! [Nick]: this acc has no named! [Nick]: this acc has no named! [Nick]: this acc has no named! [Nick]: this acc has no named! [Nick]: Account: userdata: 02000143 not logged in [Nick]: Account: userdata: 02000144 not logged in [Nick]: Account: userdata: 02000145 not logged in [Nick]: Account: userdata: 02000275 not logged in Link to comment
csiguusz Posted October 24, 2013 Share Posted October 24, 2013 (edited) Try this. function getNicks(player, command, nick) local accounts = getAccounts () local result for i, v in ipairs ( accounts ) do local named = getAccountData ( v, "name" ) if named then if named == nick then local nameacc = getAccountPlayer(v) result = true if nameacc then local name = getPlayerName( nameacc ) outputChatBox("[Nick]: Player: ".. named ..", real nick: ".. name ..", account: ".. getAccountName ( v ) .."", player, 0,175,255, false) else outputChatBox("[Nick]: nobody is logged in with this account", player, 0,175,255, false) end break else --outputChatBox("[Nick]: no result", player, 0,175,255, false) end else --outputChatBox("[Nick]: this acc has no named!", player, 0,175,255, false) end end if not result then outputChatBox("[Nick]: no result", player, 0,175,255, false) end end addCommandHandler("getNick", getNicks) Edited October 24, 2013 by Guest Link to comment
TAPL Posted October 24, 2013 Share Posted October 24, 2013 who is source? It's player not source. function getNicks(player, command, nick) If you want only online accounts then why you need to loop through all accounts in the server? rather than that you can loop through all player in the server and get their account function getNicks(player, command, nick) if not nick then outputChatBox("[Nick]: Please specify a nick!", player, 0,175,255, false) return end iii = false for i, p in ipairs(getElementsByType("player")) do local playerAccount = getPlayerAccount(p) if playerAccount and not isGuestAccount(playerAccount) then local named = getAccountData(playerAccount, "name") if named and string.match(named, nick) then local name = getPlayerName(p) local accName = getAccountName(playerAccount) outputChatBox("[Nick]: Player: "..named..", real nick: "..name..", account: "..accName, player, 0, 175, 255, false) iii = true end end end if not iii then outputChatBox("[Nick]: failed to find a match!", player, 0, 175, 255, false) end end addCommandHandler("getNick", getNicks) Link to comment
.:HyPeX:. Posted October 24, 2013 Author Share Posted October 24, 2013 should i be able to use "string.gsub(getPlayerName(p), "#%x%x%x%x%x%x", "")"? since not all players have non-coloured nicknames PD: didnt knew how to get online players.. whats the function? EDIT: tried this.. but it fails. function getNicks(player, command, nick) if not nick then outputChatBox("[Nick]: Please specify a nick!", player, 0,175,255, false) return end iii = false for i, p in ipairs(getElementsByType("player")) do local playerAccount = getPlayerAccount(p) if playerAccount and not isGuestAccount(playerAccount) then local named = getAccountData(playerAccount, "name") if named and string.match(string.gsub(named, "#%x%x%x%x%x%x", ""), nick) then local name = getPlayerName(p) local accName = getAccountName(playerAccount) outputChatBox("[Nick]: Player: "..named..", real nick: "..name..", account: "..accName, player, 0, 175, 255, false) iii = true end end end if not iii then outputChatBox("[Nick]: failed to find a match!", player, 0, 175, 255, false) end end addCommandHandler("getNick", getNicks) Link to comment
.:HyPeX:. Posted October 24, 2013 Author Share Posted October 24, 2013 Okay it failed becouse it cap-sensitive, i will try to make it avoid that, so far it works. Thanks for all the help HyPeX 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