Hello all, I ran into trouble when trying to create some kind of level queuing system. Not entirely familiar with dictionary, but the pseudo idea is like the following.
QueueTable = {
["LevelName1"] = {
[1] = { Player1, Player2, Player3 } --// Group 1 players will be teleported to another game instance and so on
[2] = { Player1, Player2, Player3 }
}
["LevelName2"] = {
[1] = { Player1, Player2, Player3 }
}
}
Players are able to select which level they want to play, and it puts them into a queue. If there is no existing group in that level, it will create a new group. If there is an existing “unfilled” group, it will fill that group to the max first.
Here’s what I have so far.
-- [[ Modules ]]
local Module_MapLevelData = require(ServerStorage.Modules:WaitForChild("MapLevelData"))
-- [[ Remote Events ]]
local teleportEvent = ReplicatedStorage.RemoteEvents:WaitForChild("TeleportEvent")
-- [[ Variables ]]
local queueTable = {}
local MAX_PLAYERS = 2
-- [[ Functions ]]
--// Get all levels into an empty dictionary at the start
for level, image in pairs(Module_MapLevelData.MapTable) do
queueTable[level] = {}
end
local function GrabPlayerLevelSelection(player, levelName)
--// Insert data if table is empty
for level, group in pairs(queueTable) do
if level == levelName then
print("Iterating "..level)
if queueTable[level][group] == nil then
print("Group not found, inserting new group: "..player.Name)
queueTable[level][group] = {player}
else
for groupKey, player in pairs(group) do
print(#group)
if #groupKey == MAX_PLAYERS then
else
--group[groupKey] = player
end
end
end
end
end
print(queueTable)
--// The rest of the teleporting players code will be down here
end
--// This event is fired from a local button click
teleportEvent.OnServerEvent:Connect(GrabPlayerLevelSelection)
It isn’t really working as intended. The goal is to insert players properly into respective table/dictionary. I’m a little confused as to how to read and create values within dictionaries, any insight is greatly appreciated!
So, a player fire a remote event from a GUI button. The player sent its own level. You want to find that level in your Dictionary, then group those players based on that Level, then teleport them all together as a group to another place when MAX_PLAYERS variable is met, right?
I made a small script that could fix your issue. Make sure to read the comments too!
I didn’t read the post carefully, here’s the version of script that will add player to a level if it has a free space (not a level with a free group)
local maxPlayers = 3
local maxGroupsPerLevel = 2
local example = {
["Example1"] = {
[1] = { "Player1", "Player2", "Player3" },
[2] = { }
},
["Example2"] = {}
}
-- getting group with smallest amount of players
local finalResult = 99999
local finalGroup
local exampleLevel = example["Example1"] -- this is the level that player selected
if #exampleLevel < 1 then -- if the list is blank
for x = 1, maxGroupsPerLevel do -- we are adding groups
exampleLevel[x] = { }
end
finalGroup = exampleLevel[1]
else
for _, group in pairs(exampleLevel) do -- elseif we are looping through the level
if #group < finalResult and #group < maxPlayers then -- if the group has smaller amount of players than the previous one and the group is not full then
finalGroup = group
finalResult = #group
end
end
end
print(finalResult, finalGroup)
if finalGroup ~= nil then -- if there is a free group then
table.insert(finalGroup, "InsertedPlayer")
end
print(example) -- visualization
For better understanding, you just need to have a level table (like this)
local example = {
["Example1"] = {},
["Example2"] = {}
}
I have studied what you provided and made it to work accordingly, thanks a lot again! Marked as solution. Hope it also helps future fellow developers~
It did took me a good hour to understand it. As the end result initially just kept filling in 2nd group, I put a break in the statement to end the for loop so it wouldn’t loop again and put the player in the non-filled group.
Also learned that we can assign table into another variable, totally forgot about this method of applying!