Help with queue script

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    Simple, I just need some help with the my 1v1 queue script
  2. What is the issue?
    I put the queue script in a local script, when the player is added queue it is client sided, not only that but I am inexperienced with remote events so I need help to make it server sided
  3. What solutions have you tried so far?
local player = game.Players.LocalPlayer
local event = script.Parent.QueueEvent
local inQueue = {}

script.Parent.MouseButton1Down:Connect(function()
	if table.find(inQueue,player) then
		for i = 1,#inQueue do
			if inQueue[i] == player then
				table.remove(inQueue, i)
			end
		end
		script.Parent.Text = "Join Queue"
		for i,v in pairs(inQueue) do
			print(v.Name)
		end
	else
		table.insert(inQueue,player)
		script.Parent.Text = "Leave Queue"
		for i,v in pairs(inQueue) do
			print(v.Name)
		end
	end
end)

No problem, events are not hard to implement.

In this case, consider a RemoteEvent object, here is a good guide for your troubles:

Any Questions?

It is good you getting started to use events, as it can be a huge life saver.

1 Like

To make this server-sided, hold inQueue{} in a script in ServerScriptService. Create a “RemoteEvent” titled whatever you want (I’ll use “Queue” for the following example) inside ReplicatedStorage. Now we reference this event in both the server and client. Put something like these lines on both the server and local script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local QueueEvent = ReplicatedStorage:WaitForChild("Queue")--we wait for the child so we don't have a mishap that the script runs before it's instanced and creates an error

In your localscript, you don’t need to house the table anymore, so I would change it to a boolean so the client can keep track if they know they’re in queue or not. Now your localscript would look something like:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local QueueEvent = ReplicatedStorage:WaitForChild("Queue")
local inQueue = false

script.Parent.MouseButton1Down:Connect(function()
	if not inQueue then--player not in queue
		inQueue = true
		script.Parent.Text = "Leave Queue"
	else--player in queue
		inQueue = false
		script.Parent.Text = "Join Queue"
	end
	
	QueueEvent:FireServer()
end)

Now your server script will listen for the event and act accordingly, such as:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local QueueEvent = ReplicatedStorage:WaitForChild("Queue")
local inQueue = {}

QueueEvent.OnServerEvent:Connect(function(ply)
	if table.find(inQueue, ply) then
		table.remove(inQueue, table.find(inQueue, ply)) -- as table.find returns the index, we don't need to iterate
	else
		table.insert(inQueue, ply)
	end
	
	for i,v in pairs(inQueue) do
		print(v.Name)
	end
end)

Here, the function inside of the connection will only execute if the remote event is triggered, meaning a player’s script has fire at it to tell it they’ve pressed the queue button.
*Edit: mixed up how the button’s text responds

1 Like