Discern
|
  |
| Joined: 02 Feb 2013 |
| Total Posts: 331 |
|
|
| 13 May 2014 03:26 PM |
I want to use table.remove, but unlike table.insert, you need the position of the value.
How can I get the position of the value if I don't know where it is?
For example, if I know I will be in the game when this script runs:
playersingame = game.Players:GetPlayers()
table.remove(playersingame, "Discern")
That would not work because I need the position, but I have no idea where it is.
How would I find the position of my name? |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 13 May 2014 03:27 PM |
for key = 1, #playersingame do if playersingame[key] == "Discern" then table.remove(playersingame, key); end end |
|
|
| Report Abuse |
|
|
|
| 13 May 2014 03:29 PM |
function indexOf(Table, Value) for _, v in pairs(Table) do if v == Value then return _ end end return nil end
local ind = indexOf({a=1, b=2}, 2) --> "b" |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 13 May 2014 03:35 PM |
What's the point of returning nil O_o And I just assumed "only numerical keys" since "GetPlayers" |
|
|
| Report Abuse |
|
|
|
| 13 May 2014 03:39 PM |
| nil is the only key that is invalid for tables. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 13 May 2014 06:26 PM |
Yes but if you don't return anything it will be nil either way.
local x = function() end;
print(x()); is going to give you nil |
|
|
| Report Abuse |
|
|
|
| 13 May 2014 07:45 PM |
Ah yes. It's more of a preference of mine to return nil instead of return (or just let the function end). |
|
|
| Report Abuse |
|
|