Dueling Random Selection Help

So, I’m attempting to make a dueling game - the part that selects two people at random is the rough part. I have an idea of what I’m attempting to write - its just that its hard to put it in terms of actual code.
So in my notes I have -
Check if two people in the game.
Grab two random people from said players
If they are already chosen, or in the midst of combat don’t select ( I’m thinking of using a table of some type of array to know who is dueling currently, or maybe even a team? )
After the fight concludes, return those players back to selectable category. ( so i.e take away from candidates table. )

My real issue is just putting this into code, I’m not asking to be fed the answer, maybe suggestions and good resources would helpful. Thanks.

1 Like

Hiwi!
Well you already have a very clear idea of what to do. I dont think you will have any problem with your system, you already visualized it.

I would to this.
Store in a table all players inside the server, or letting each player to decide when he’s ready to fight or when they join server.
By clicking a button on object or GUI button, the player can choose if is ready to fight, so add it to a main table, server side.
Ppl likes dictionaries, I prefer arrays inside arrays. Just place a bool var on that table for each fighter, turn on/off true/false when they are on fight, and when they are free to fight
Use math.random to choose a couple of players from the table, check its bool, if they are free, take them, put them to together.
When the player left server, remove completely his entry.

I think all your notes are great!
Documentation? well, probably you already know the basic one you will need.

2 Likes

Well, I would start out by making a bool value for the player, so maybe put a bool value into StarterGui and name it something like “inMatch” or something.

Then in the script I would make a for loop and then check if that value is true for two players, and then
make the “inMatch” variable true and assign the two players to variables or insert them into a table and wait until one of them dies to end the match and reset the “inMatch” value to false.

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!

2 Likes