✓ Accepted answer
#What you could do
Make a dictionary. Dictionaries are tables with custom keys. You could make the keys the answer choices and the values the amount of times they're selected.
#Dictionaries
When you make a dictionary you'd make you custom key by surrounding what would be your key with square brackets []. Your key itself can be any lua value. Such as a float, boolean or even an object. As long as it isn't nil. And since a string isn't nil we can use it!
local t = {["Key"]}
Now we need to set the key's value. We'll set it to 0 in this example.
local t = {["Key"] = 0}
Now whenever we check the value for Key, we'll get 0.
print(t["Key"])
0
#Final Product
local exampledatastore = Datastore:GetDataStore(example)
ExampleTable1 = {}
ExampleTable2 = {}
ExampleTable3 = {}
for table,v in pairs(exampledatastore) do
if table == 1 then
for key,value in pairs(exampledatastore[v]) do
ExampleTable1[key] = value
end
elseif table == 2 then
for key,value in pairs(exampledatastore[v]) do
ExampleTable2[key] = value
end
else
for key,value in pairs(exampledatastore[v]) do
ExampleTable3[key] = value
end
end
end
popularanswer = {}
number = 0
for i,v in pairs(ExampleTable1) do
number = math.max(number,v)
table.insert(popularanswer,i)
end
number = 0
for _,v in pairs(ExampleTable2) do
number = math.max(number,v)
table.insert(popularanswer,i)
end
number = 0
for _,v in pairs(ExampleTable3) do
number = math.max(number,v)
table.insert(popularanswer,i)
end
print("The most popular answers were as follows: "..table.concat(popularanswer, ", ")..".")
Hope it helps!
3 comments
for loop
I understand that I need a for loop but I can't figure out how I would count the values individually and find the one that shows up the most.
If I don't know anything about the values in the table beforehand expect the fact that they are strings.