Turn values into table entries


I’m currently working on a player voting system. How it works is that when a player clicks on a player’s button, it will add one vote to an assigned IntValue of their name.

I’m trying to add these to a table so I can randomize the votes. Is it possible to add these votes to a table with the same entry amount as their value?

Thank you for the help in advance.

local ValueFolder = --put folder with values here
local tableloc = {}
for i,v in pairs(ValueFolder:GetChildren()) do
    if v:IsA("IntValue") then
        table.insert(tableloc, v.Value)

The above code will loop through a folder with integer values and grab each value to add to the table. If this is not your use case, the general way to go about doing it is to grab the value doing value.Value and then inserting it into the table with table.insert(tablelocation,value.Value)

Good luck!

This is useful, however you mistook what I said. I don’t need to insert the value, I need to insert something as many times as the value. Thank you though!

Like a loop?

for 1, value.Value, do
    table.insert(table, valuehere)

Thank you for that! I didn’t know you could substitute Values in pairs loops. However, how can I run it so all the IntValues get duplicated like this? Do I put it in a loop as well?

The values are named differently so I want to insert all the value names with different value amounts without having to write it all out.

Yea I would put all your values in a folder and loop through the folder for each value, like I did in my first example.

That’s actually what I’m already doing. Thanks!

So to an assigned int value of their name, as in with associating a player’s name with their value?

   -- in a local script
  local event = game:GetService("ReplicatedStorage").Event

  local button = script.Parent
  clicked = false
  cooldown = 3
  amount = 1

         if not clicked then 
            clicked = true
           clicked = false

Then when you receive the value on the Server, change the corresponding player’s value in a dictionary of players

-- Server Script

  local players = game:GetService("Players")
  local event = game:GetService("ReplicatedStorage").Event
  votes =
      --[[ would look like 
        ["Player"] = 2; 
        ["Player3"] = 30; ]]
  -- Setting up a dictionary, where every player's name is a string index, value being their votes' value

  for _, player in ipairs(players:GetPlayers()) do
         votes[player.Name] = player.SomeFolder.Votes.Value  

  event.OnServerEvent:Connect(function(player, amount)
        votes[player.Name] = votes[player.Name] + amount

Note that the dictionary could’ve been created before any other players join , thus would be outdated, so you would need to update the values in the dictionary (if that is what you intended on doing) when the value of their Votes changes, or a player leaves or another joins.

Do note that the above example is a demonstration only, suit it to your needs.


Ipairs is better suited for iteration over numerical indices,thus is faster. Pairs is designed to work with dictionaries.

1 Like

Couldn’t I just do:

local tab = {}

for i,v in pairs(folder:GetChildren()) do
    for j,v.Value in pairs do
        table.insert(tab, v.Name)

I haven’t tested it but I think this may work. If not, then I’d love for you to explain.