You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I wanna be able to use for loops with the dictionaries.
What is the issue? Include screenshots / videos if possible!
I used a for loop but it just didnt run.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I printed the length of the table with # operator but it just said 0.
local Test = {
Test1 = 1;
Test2 = 2;
Test3 = 3;
}
print(#Test)
The unary length operator # only gets the length of a table’s array part. Your table only has a dictionary part therefore its length is 0.
If you need to get how many key-value pairs there are, then use this helper function:
local function len(t)
local n = 0
for _ in pairs(t) do
n = n + 1
end
return n
end
If you need to loop through a dictionary, use this:
for i, v in pairs(dictionaryHere) do
end
caviarbro
(caviarbro)
June 29, 2021, 3:40am
#3
local Amount = 0
local Test = {...}
for i,v in pairs(Test) do
Amount += 1
end
print(Amount)
To get the length of a dictionary, use this function:
local function getDictionaryLength(dict)
local length = 0
for k, _ in pairs(dict) do
length += 1
end
return length
end
It works with any dictionary entry type, such as:
local dictionary = {
['hi'] = 8,
'lol',
'bye',
['no'] = 0
}
It worked fine for me… any errors?
works now but the for loop only loops through one value, not three
What dictionary are you inputting?
well it works in the script i tested but not if i get a table from a modulescript
caviarbro
(caviarbro)
June 29, 2021, 3:54am
#10
Can you print the table and see what you get? It is probably because the table is nil.
Oh i fixed it. Basically I used a RemoteEvent and fired an event from the localscript with the type of modulescript. I forgot that modulescripts are different for the server and client.