Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I see if a name exists in a table, without having to iterate through every value?

Asked by 8 years ago

So, I basically need to see if a name exists in a table, but I can't iterate through all the values. Is it possible?

1
[ALERT I TOLD HIM TO MAKE THE QUESTION DUE TO IT WAS IN THE CHAT] MessorAdmin 598 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago
local PlayersData={
    ['MessorAdmin'] = "derp";
    ['AwseomeSpongebob'] = "Hellooooooo? nub?"
}
 -- Alright, here's our table.

We want to print our data right? Lets go ahead and do that now. There's a few different ways. Way 1:

Not meant for new games, it's deprecated.

table.foreach(PlayerData, print)

Way 2:

for i,v in next,PlayerData do
    print(i, v)
end

Way 3: Now, if you don't want to go all the way through all the datas just use this.. But be careful, this is prone to errors.

local Result;
if PlayersData['MessorAdmin'] then
    Result = PlayersData['MessorAdmin']
else
    Result = "Aww :("
end
print(Result)
Ad

Answer this question