Getting a table from a RemoteEvent

Hello everyone!

I’m currently working on a queue system, where a player can touch a part (further referred to as “queuePart”) to be added to the queue system, and then click a GUI to be removed from the queue system.

The first half was easy; however, the latter half is a bit more challenging.

All the players currently stored in my queue are part of a table called playersInQueue. They are stored by username and are added when they first touch the part. However, as the GUI to leave the queue is client based, I need to use RemoteEvents to edit the table so the player is removed based on username, so the LocalScript in the GUI can communicate with the ServerScript of the queuePart.

How would I write a script so that when a GUI is clicked, it can access the table in my main script and check if the player is in the table (which if they see the GUI, they 100% are in the table)?

Thanks in advance.

RemoteEvents will automatically pass the firing client as the first argument. Knowing this, when you connect to a RemoteEvent from the server, you will already know who the player requesting to leave the queue is by Player object. Accessing their name property will give you their username.

local function serverEvent(player)
    print(player.Name)
end

remoteEvent.OnServerEvent:Connect(serverEvent)

It would be a matter of using this given name, searching through your table and if they’re there, removing their name from it. If you have an array for playersInQueue, you can use table.find to see if the player name exists in the table and if it does, pass what it returns to table.remove.

Some details about why the above works: table.find finds a value in a table and if it exists, returns the position of the value in the array as a number. table.remove can only remove by position, not by value, but table.find gives you the number you need. Therefore, it’d all come down some code like:

local playersInQueue = {"foo", "bar", "qaz"}

local function serverEvent(player)
    local playerName = player.Name
    local queuePosition = table.find(playersInQueue, playerName)

    if queuePosition then
        table.remove(playersInQueue, queuePosition)
    end
end

remoteEvent.OnServerEvent:Connect(serverEvent)

In my own personal experience, since the advent of CollectionService, I would always use it for systems like this. Assuming that you’re making this for one of those story games or a similar teleporter, players entering the queue would be tagged with “InQueue” or something and players wanting to leave would fire a remote that removes the tag. When it’s time to teleport, GetTagged on InQueue and pass it to a teleport method.

Only difference with the above method would be having to generate or fetch (have not tested memory addresses yet) the array every time I want queued players for anything and there’s an insignificant amount of extra memory because of serialising those tags on instances. Good stuff otherwise.

4 Likes

This is a very in depth solution; however, one small clarification:

Would this be the ServerScript which is a child of the RemoteEvent? Or would this be a separate script, such as in ServerScriptService?

Thank you for taking the time to explain this thoroughly. :smiley:

The RemoteEvent should be anywhere in ReplicatedStorage so that both the client and the server can see and access it. Any deeply structured game may put then in a remotes folder or similar. Only must is that it has to be a descendant of ReplicatedStorage.

The script hooking the server-side of the remote should be in ServerScriptService. This is the canonical location for all scripts now and makes perfect sense as well. If your queue system is already set up in a script then I would just do the event connection from that script so you have direct access to the playersInQueue table and because as a part of the system, it’d logically make sense to put it there.

1 Like