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?
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)
end
end
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)
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!
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.
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
button.MouseButton1Click:Connect(function()
if not clicked then
clicked = true
event:FireServer(amount)
wait(cooldown)
clicked = false
end
end)
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
end
event.OnServerEvent:Connect(function(player, amount)
votes[player.Name] = votes[player.Name] + amount
end)
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.