You could do that, but there is probably a more practical (perhaps) and more exploiter proof solution. @Dev_Peashie wrote a pretty comprehensive post about this. You store an event in ReplicatedStorage and activate it when an option on GUI button is activated. When firing a server on button press, you send additional information with it, including players decision about joining the match (which is either true or false). Player’s name is sent by default. So how to do this?
local remoteEvent = """event path here"""
remoteEvent:FireServer("""put boolean value here""")
Server accepts this (take some precaution and set up sanity checks as well as debounce):
remoteEvent.OnServerEvent:Connect("""function that processes received data""")
So you store new player and his decision in an array or a dictionary. This is your choice. There really are different ways to do it. For example like the following:
local PlayerStatuses = {
{"player_1", true, true}; -- player's name or id, does player want to participate, is he/she in match
{"player_2", true, false};
}
How do you retrieve data from here?
PlayerStatuses[1] equals the first element in the array, so:
local playerData_first = PlayerStatuses[1]
local matchStatus_first = playerData_first[3]
Or you can use arrays inside arrays, such as:
local PlayerStatuses = {
InMatch = {"player_1", "player_2"};
Participants = {"player_1", "player_2", "player_3"};
Non_Participants = {"player_4"};
}
Retrieving data from this is pretty much the same:
PlayerStatuses[2] means Participants array
And also other ways to store such data. Some provide faster data access than others. Dictionaries are a little different, but they might be the fastest of all. Either way, you loop through a list and see which player is available in a match. When player finishes the match, remove them from a table, when they start the match, add them to it, or change values inside a table (first example).
math.random
is the right choice. According to the length of a list, set in what range the random number is returned. math.random(5)
will give you a random number between 1 and 5. If random number repeats, try again, and so on.
I really suggest you check the links in Dev_Peashie’s post, because you can learn a lot from reading those pages.
Good luck with your project!