Player queue script

The game I’m working on will require a queue system, as 2 players enter a 1v1 scenario in front of all other players in the server. One player will challenge another, and should the other accept, it will place them into a queue to be given their match. I was about to try and take a shot at this by myself before I realized that the way I’m attempting this is probably not the best, and will be a mess by the end of it, so I’d like some input and advice beforehand.

local battleQueue = game.ReplicatedStorage.BattleQueue

local queue = {
	
	["Position 1"] = 1,
	["Position 2"] = 2,
	["Position 3"] = 3,
	["Position 4"] = 4,
	["Position 5"] = 5,
	["Position 6"] = 6,
	["Position 7"] = 7,
	["Position 8"] = 8,
	["Position 9"] = 9,
	["Position 10"] = 10,
	
}

battleQueue.OnServerEvent:Connect(function(Player, name)
	print("Adding players to queue")
	
	
end)

There will be 20 players per server, so there should, at most, be 10 positions considering that it will always be a 1v1. Passed on variables are as follows: Player being the player who challenged the other, and name being the player who accepted the challenge.

My idea was to try and assign both players to one variable, then assign that variable a position in the queue. This would require an automated variable assignment, which I’m honestly unsure of how to do just yet, although I am thinking of using math.random for a range of 1-10. Either way, once the variable has been assigned it would replace the string “Position (number)” with said variable, with the value being position in the queue.

Would there be a simpler way to go about this? Please let me know. If this is a good way of doing it, how could I automate variable assignment for the two players?

1 Like

If I am understanding correctly:

Assign 2 players (that have confirmed a duel) to a place in a table:

local info = {player1, player2} --should be of type "Player"
table.insert(Queue, info)

when the current duel is finished:

--inside of main game loop...
local current_duel = table.remove(Queue, 1) 
--removes the current duel from the queue 
--and holds it in a variable

--perform operations with players...
--accessing players
current_duel[1].Character.PrimaryPart.CFrame = Spawnlocation.CFrame
current_duel[2].Character.PrimaryPart.CFrame = spawnlocation2.CFrame
--or however you might do this

2 Likes

Yeah this is WAY better than what I had planned with the dictionary, thank you so much. I just need to figure out how to remove the match from the queue in the case of one player leaving, but it shouldn’t be too hard. I think I can manage this with game.Players.PlayerRemoving. As for adding to the queue, this is what I have:

local battleQueue = game.ReplicatedStorage.BattleQueue

local queueTable = {}

battleQueue.OnServerEvent:Connect(function(Player, name)
	print("Adding players to queue")
	
	local info = {Player, name}
	table.insert(queueTable, info)
	
end)

I didn’t know you could add a table into a table, but it’s definitely a really good way of assigning 1 variable to 2 objects. Thank you so much.

1 Like

You can additionally add an ID to each instance in the queue.

--when duel created....

local ID = tick() --literally cannot be the same, dw about "Same time" duel creations

--add the ID to the player's attributes:

player1.Character:SetAttribute("DuelID", ID)

local info = {player1, player2, id = ID}

table.insert(Queue, info)

--detect player leaving, and get the player who is leaving
game.Players.PlayerRemoving:Connect(function(playerWhoLeft)
	
	local duelEnded --temp nil variable for storing the empty duel
	
	
	for i,v in pairs(Queue) do
		--for each element in the queue do...
		--if the id of the match is the same as the player's who left then...
		if v.id == (playerWhoLeft.Character:GetAttribute("DuelID") then
			for x,c in pairs(v) do
				--make sure we are checking a player value, if we are checking the ID we will get an error
				if c:IsA("Player") then
					--if the other player is still in the server and the name is different than the player who left then...
					if c.Name ~= playerWhoLeft.Name and c then
						--set other player's ID to nil or 0
						c.Character:SetAttribute("DuelID", nil)
					end
				end
			end
			--remove the match from the queue table, ending the duel.
			duelEnded = table.remove(Queue, i)
		end
	end
	
	--announce or do whatever with the information from the ended duel:
	
	print(playerWhoLeft, " Left and ended the duel between " ,duelEnded[1], " and ", duelEnded[2] "!")



end)


This ID system can allow for checking if a player is already in a duel too.
Important: make sure you are adjusting the attribute of the SAME INSTANCE YOU SET IT AS because c:GetAttribute() and c.Character:GetAttribute() are not the same!

2 Likes

Never thought of assigning IDs but its really smart! I’ve also never worked with SetAttribite before so this made for a good lesson on it as well. I do need to work a bit more on learning loops because trying to implement this made me realize that loops really aren’t my strong-spot. I didn’t want to just copy-paste the code because I wanted to learn, and this taught me really well. Thank you for this, it means a lot man.

1 Like

No problem! if you want help with your game reply here and I’ll send my disc user. I’d be happy to help you out!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.