Hello there.
I am here because I encountered some trouble when it came to creating a queue system similar to that game.
I have tried several attempts at creating that myself, yet I faced a tremendous amount of problems when it comes to creating it, such as when you play it in multiplayer, the queue system just crashes itself, leaving players stuck in two different queue positions that are not even closer to the maximum position.
Here is my current progress so far:
local LevelSquareSlots = workspace.LevelSquareSlots -- Container holding each of the slots (1-50 in this case)
local queue = {} -- List of players in the queue, starting from the back (position 50)
-- function to move the player towards a specified position within the queue
-- step by step
local function moveTo(humanoid, j, targetPosition)
local function moveFunction(i : number)
humanoid:MoveTo(LevelSquareSlots:FindFirstChild(tostring(i)).Movement.Position)
humanoid.MoveToFinished:Wait(2)
end
if j < targetPosition then
for i = j, targetPosition do
moveFunction(i)
end
else
for i = j, targetPosition, -1 do
moveFunction(i)
end
end
end
-- Function to move the player to a specific position in the queue
local function moveToPosition(character, targetPosition)
local humanoid = character:FindFirstChildOfClass("Humanoid")
if humanoid then
local targetSquare = LevelSquareSlots:FindFirstChild(tostring(targetPosition))
if targetSquare then
moveTo(humanoid, character:GetAttribute("Position"), targetPosition)
character:SetAttribute("Position", targetPosition)
print(character.Name .. " is moving to square " .. targetPosition)
end
end
end
-- Function to update the queue
local function updateQueue()
task.spawn(function()
for i, player in ipairs(queue) do
local character = player.Character
if character then
-- Move each player to their respective slot (51 - i)
local position = 51 - i
moveToPosition(character, position)
print(player.Name .. " is now at position " .. position)
end
end
end)
end
-- Function to find the next free position in the queue
local function findNextFreePosition()
for i = 50, 1, -1 do
local occupied = false
for _, player in ipairs(queue) do
if player.Character:GetAttribute("Position") == i then
occupied = true
break
end
end
if not occupied then
return i
end
end
return nil -- Return nil if the queue is full
end
-- Function to join the queue
local function joinQueue(player)
local character = player.Character
if character then
local nextPosition = findNextFreePosition()
if nextPosition then
table.insert(queue, player) -- Add player to the queue list
updateQueue() -- Update the queue positions
print(player.Name .. " joined the queue at position " .. nextPosition)
else
print(player.Name .. " could not join the queue: Queue is full.")
end
end
end
-- Function to leave the queue
local function leaveQueue(player)
for i, p in ipairs(queue) do
if p == player then
table.remove(queue, i) -- Remove the player from the queue
print(player.Name .. " left the queue.")
break
end
end
updateQueue() -- Update the queue after removal
end
-- Function to skip ahead in the queue
local function skipQueue(player)
for i = 2, #queue do
if queue[i] == player then
-- Move the player ahead of the person directly in front of them
local targetPlayer = queue[i - 1]
table.remove(queue, i) -- Remove from current position
table.insert(queue, i - 1, player) -- Insert ahead of the previous player
print(player.Name .. " skipped ahead of " .. targetPlayer.Name)
updateQueue()
break
end
end
end
plr.CharacterAdded:Connect(function(character)
character:SetAttribute("Position", 1) -- Players start at position 50
joinQueue(plr) -- Join the queue when character is added
end)
Please bear in mind that playing alone wouldn’t cause such an issue, but when playing it in multiplayer, especially with let’s say 50 players in a server, that could be quite a different story.
This is when playing alone:
And this is when playing with two or more players:
Some output as well:
01:46:12.901 Player1 joined the queue at position 50 - Server - Queue:83
01:46:12.947 Player2 joined the queue at position 50 - Server - Queue:83
Thank you so much for taking the time to read this; any help is appreciated.