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

How can I check to see if something is a table in my script?

Asked by 6 years ago

I want to make sure what my datastore returns is a table and if it is anything else to just ignore it. Would :IsA work? If so I dont know how to make that work. I dont need you to write me any code. I just want to know an operator or something that would check. Thank you in advance.

0
type(thing) == "table" theCJarmy7 1293 — 6y
0
Yeah I don't think posting an answer form is necessary on this one. What you're looking for is the built-in type function, as CJarmy mentioned. You can read more about it here: https://www.lua.org/pil/2.html ScriptGuider 5640 — 6y
0
Thank you User#21908 42 — 6y

1 answer

Log in to vote
0
Answered by
Amiaa16 3227 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

You can use an if statement like this:

local data = DataStore:GetAsync("key")
if not data then
    data = {}
end

But you can also use the or operator instead

local data = DataStore:GetAsync("key") or {}

It will set the variable to {} if GetAsync() returns nil

Oh and if you want to check specifically for a table, use typeof()

local data = DataStore:GetAsync("key")
if typeof(data) ~= "table" then
    return
end
0
Thanks yeah I just want to check and see if it is a table. Not if its nil. User#21908 42 — 6y
Ad

Answer this question