generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: How to find the position of a value in a table?

Previous Thread :: Next Thread 
Discern is not online. 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 is not online. 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
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
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 is not online. 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
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
13 May 2014 03:39 PM
nil is the only key that is invalid for tables.
Report Abuse
cntkillme is not online. 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
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image