There’s such thing as a mixed table. This is not a very reliable check. typeof
is also ridiculously slow, so I’d recommend type
where possible. Also, please don’t edit in a reply that answers a reply further down in the thread, it breaks the continuity of the thread.
The typeof was just because I’m used to using that for validating Roblox-specific datatypes, type should be used here probably.
Obviously not reliable, that’s the reason why I said it would only work for defining ‘typical’ tables (where it is either a dictionary or an array, was only a quick edit to my post), for example using that algorithm it would print {}
as a dictionary which it technically isn’t.
I left an extra end there by mistake
Anyways, since I don’t have access to Studio, I wrote this in Lua 5.3.5
local tab = {1, nil, 1}
local function nilfound(t)
local i = 0
for _, _ in pairs(t) do
i = i + 1
end
if i - #tab ~= 0 then return true end
end
setmetatable(tab, {
__tostring = function(self)
if nilfound(self) then return "nil found, cannot perform operation" end
local indexes, result, numericalindex = {}, nil, nil
for k, _ in pairs(self) do
if type(k) == "number" then numericalindex = true end
if type(k) ~= "number" and numericalindex then return "mixed table" end
table.insert(indexes, k)
end
if #indexes == 0 then return "empty table" end
if #self ~= #indexes then result = "dictionary"
else
result = "array"
end
return result
end
})
print(tab) --> cannot perform operation
Edit:
+This code was optimized to work better for various cases.
Just adding this here: typeof
was optimized in Luau
@ zeuxcg
Optimize
typeof()
to run ~6x faster. It used to be thattype()
was much faster thantypeof()
but they now should be more or less comparable.
???
Where in my post does it say a table is not a valid type, or that an array and a table are different types?
I was only suggesting that saying
type(t)
would return anything different from what calling type(t) would return where t is either a table or array is incorrect.
I’m just saying even if it were an array, type(t) would still return “table” so I won’t say that’s a reliable check to differentiate between a numerically indexed table or a dictionary.
Keep in mind that even this convoluted of a check will still consider local tab = {1, 2, nil, 3, [true] = "there"}
an array, so there’s not an ideal way to check for this.
Considers almost every case possible, if the table contains nil then it’ll not do anything because it’s not any of the three, (for the nil part, I had to resort to the forum and an unexpectedly simple function).
Hey i have a question, how i can get only one value, like
for i,v in pairs(script.Workspace:GetChildren()) do
if v:IsA("BasePart") then
print(v.Name) -- but only get the name of the first value it found
end
Iterating isn’t always necessary, ::GetChildren() returns an array, so just use the first returned object
local firstPart = workspace:GetChildren()[1]
if firstPart:IsA("BasePart") then
print(firstPart.Name)
end
But if you want it to keep iterating until a BasePart is found, hence print the name only once:
for _,v in ipairs(workspace:GetChildren()) do
if v:IsA("BasePart") then
print(v.Name)
return -- if the eval returns true, return end and stop iterating
end
end
Also, how on Earth did you manage to parent the workspace to a script!?
I believe you meant to instead just use the shorthand workspace, or game.Workspace.
Thanks this help me a loot [quote=“XxELECTROFUSIONxX, post:28, topic:626905”]
Also, how on Earth did you manage to parent the workspace to a script!?
I believe you meant to instead just use the shorthand workspace, or game.Workspace
[/quote]
Srry I copy this from a script I have and change this a bit, but again thanks a loot