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 do you check a table?

Previous Thread :: Next Thread 
LX7 is not online. LX7
Joined: 19 Apr 2011
Total Posts: 1533
19 Jul 2011 09:51 AM
How do you check a table if it has a certain index? As in a string,object,ect.
Report Abuse
Combrad is not online. Combrad
Joined: 18 Jul 2009
Total Posts: 11025
19 Jul 2011 09:52 AM
Could you use pcall and then use tostring() tonumber() and if it errors, thats the type it is.
Report Abuse
LX7 is not online. LX7
Joined: 19 Apr 2011
Total Posts: 1533
19 Jul 2011 09:54 AM
Example?

So if I had a table :

people = {"Player", "LX7"}

How would I check if my name was in there?
Report Abuse
MARIOSTAR6464 is not online. MARIOSTAR6464
Joined: 21 Jul 2008
Total Posts: 13674
19 Jul 2011 09:55 AM
How do you mean?

I usually put [word][#] and work it like this:

Table = {"Tab1", "Tab1", "Tab1"}
print("Tab" ..math.random(1,#Table))
Report Abuse
Combrad is not online. Combrad
Joined: 18 Jul 2009
Total Posts: 11025
19 Jul 2011 09:56 AM
Oh, that. I thought you meant the type.

Do something like.

function IsPlayerInTable(table,Nombre)
for i = 1,#table do
if string.lower(table[i]) == string.lower(Nombre) then
return true
end
end
return false
end

IsPlayerInTable(Table,Name)
Report Abuse
LX7 is not online. LX7
Joined: 19 Apr 2011
Total Posts: 1533
19 Jul 2011 10:01 AM
@Combrad
Hey Combrad, I'm death. Got banned for 7 days, released on 22nd. And would this work?

people = {"LX7", "Player"}

function FindPlayer(t,p)
for i = 1,#t do
if string.lower(t[i]) == string.lower(p.Name) then
return "Clean"
else
return "BAD"
end
end
end

game.Players.PlayerAdded:connect(function(p)

test = FindPlayer(people,p)

if test == "Clean" then
--stuff
end

end)
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
19 Jul 2011 10:02 AM
Or you could do some hax with metatables, and add a table.find function.


local table = setmetatable({}, {__index = setmetatable({ find = function(tab, value) for _,v in pairs(tab) do if v == value then return _ end end return nil end}, {__index = table}), __newindex = function() end, __metatable = { } })


local myTab = {"Person", "Combrad", "LX7"}
local found = table.find(myTab, "LX7")
print(found)
--> 3 (this is the index)
print( myTab[3] )
--> LX7


      ~~~ AFF ~~~
Report Abuse
LX7 is not online. LX7
Joined: 19 Apr 2011
Total Posts: 1533
19 Jul 2011 10:03 AM
@AFF
I barely understood anything in that script except for the bottom. I'm bad with tables, learning it right now.
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
19 Jul 2011 10:04 AM
Have fun. Tables are one of my favorite data types in Lua. There's so much to learn. :P
Do you understand the basics of how tables work? I assume you do, but you may not know the proper terms to use. :P



      ~~~ AFF ~~~
Report Abuse
LX7 is not online. LX7
Joined: 19 Apr 2011
Total Posts: 1533
19 Jul 2011 10:06 AM
I only know how to make tables, insert, and index.
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
19 Jul 2011 10:44 AM
^_^


A table, in Lua, is arranged by key-value pairs. What does this mean? It means for every value in the table, its location is a specific key. When you define a table, you can explicitly set these keys. If you don't specify keys, they are understood as integers starting at 1. For example...


-- TABLE = { [KEY] = VALUE }

tab = { "Joe", "Bob", [4] = "John", Num = 10, ["Filed"] = true }
--[[

                  _____________________      
                 |                |                |
                 |     Key      |     Value    |
                 |__________|__________|
                 |                |                |
                 |       1       |       Joe     |
                 |__________|__________|
                 |                |                |
                 |       2       |     Bob      |
                 |__________|__________|
                 |                |                |
                 |        4      |     John     |
                 |__________|__________|
                 |                |                |
                 |     Num     |       10      |
                 |__________|__________|
                 |                |                |
                 |    Filed      |     true      |
                 |__________|__________|

]]


The the keys do is tell WHERE a value is stored in the table.


      ~~~ AFF ~~~
Report Abuse
Combrad is not online. Combrad
Joined: 18 Jul 2009
Total Posts: 11025
19 Jul 2011 01:49 PM
people = {"LX7", "Player"}

function FindPlayer(t,p)
for i = 1,#t do
if string.lower(t[i]) == string.lower(p) then
return "Clean"
else
return "BAD"
end
end
end

game.Players.PlayerAdded:connect(function(p)

if FindPlayer(people,p.Name) == "Clean" then
--stuff
end

end)

Just made it slightly fewer lines :D
Report Abuse
LX7 is not online. LX7
Joined: 19 Apr 2011
Total Posts: 1533
19 Jul 2011 02:13 PM
Thanks
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