I don’t get any errors but the code doesn’t seem to work anyway for some reason, I couldn’t figure out what’s wrong honestly.
function selector:Vote(player: Player, map: string)
if not player or not map then error("No player or map.") end
if not self.Queue then error("No queue.") end
for i, v in pairs(self.Queue) do
print("Searching for map..")
if v.Map == map then
print("Skipping..")
continue
end
local playerI = table.find(v.Votes, player)
if playerI then
print("Player had voted another map before, removing the vote..")
table.remove(v.Votes, playerI)
end
end
for i, v in pairs(self.Queue) do
if v.Map == map and not table.find(v.Votes, player) then
print(string.format("Assigned %s's vote to %s", player.Name, map))
table.insert(v.Votes, player)
break
end
end
print(self.Queue)
end
Can you show the output when you attempt to use the system?
The code looks like it should work, although there is little contrxt we have, as in where it is called and what arguments are passed in
local selector = {}
selector.__index = selector
function selector.Start(maps)
local self = setmetatable({}, selector)
self.Maps = maps:GetChildren()
self.mapsToVote = #self.Maps
return self
end
function selector:startVoting()
local availableMaps = {}
local Queue = {
[1] = {
Map = nil,
Votes = {}
};
[2] = {
Map = nil,
Votes = {}
};
[3] = {
Map = nil,
Votes = {}
};
}
repeat
task.wait()
for i = 1, self.mapsToVote do
local map = self.Maps[math.random(1, #self.Maps)]
--print(map)
if table.find(availableMaps, map) then
continue
end
table.insert(availableMaps, map)
end
until #availableMaps == 3
local modifiedQueue = table.clone(Queue)
print(availableMaps)
modifiedQueue[1].Map = availableMaps[1]
modifiedQueue[2].Map = availableMaps[2]
modifiedQueue[3].Map = availableMaps[3]
self.Queue = modifiedQueue
print(self.Queue)
end
function selector:Vote(player: Player, map: string)
if not player or not map then error("No player or map.") end
if not self.Queue then error("No queue.") end
for i, v in pairs(self.Queue) do
print("Searching for map..")
if v.Map == map then
print("Skipping..")
continue
end
local playerI = table.find(v.Votes, player)
if playerI then
print("Player had voted another map before, removing the vote..")
table.remove(v.Votes, playerI)
end
end
for i, v in pairs(self.Queue) do
if v.Map == map and not table.find(v.Votes, player) then
print(string.format("Assigned %s's vote to %s", player.Name, map))
table.insert(v.Votes, player)
break
end
end
print(self.Queue)
end
return selector
Problem is you are storing the Map instance in available maps, and comparing it to the map string. Try doing
if v.Map.Name ==map
Make sure to change it for both if statements
I meant that the table was using get children, which wouldnt reference the name of the map.
This makes v.Map an instance because it iterates through self.queue
Yes thats what I was saying. If you set a table to another table, it doesnt clone the original table, just references it. This is why you use table.clone
This was my fault for not making it clear.