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
|
  |
| 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
|
  |
| 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 |
|
|
|
| 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
|
  |
| 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
|
  |
| 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 |
|
|
|
| 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
|
  |
| 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 |
|
|
|
| 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
|
  |
| 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 |
|
|
|
| 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
|
  |
| 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
|
  |
| Joined: 19 Apr 2011 |
| Total Posts: 1533 |
|
| |
|