Essentially what you want to do is get 2 random players from inside the server and select them to fulfill each role. Then, you will give them each of their own abilities.
First, get the list of players in the game.
local players = game.Players:GetPlayers()
Next, we have to select the first player (the catcher) from that list.
local catcher = players[math.random(1, #players)]
What the line of code above is doing is the following: the ‘players’ variable is a table. So, it’s getting an element from that table using the square brackets []
. To select which element we want, we’re using math.random(1, #players)
, which is selecting a random number from 1 to the number of players we currently have in the game. For example, let’s imagine the number of players in the game is 5. It would be like writing math.random(1, 5)
. Now, let’s imagine the number 3 is picked. We’d be getting the 3rd player from that list (players[3]
).
Now, we remove the catcher from the list we created of the players.
table.remove(players, table.find(players, catcher))
The rest is now up to you. If you want to have every single other player be the runner, then you can use the players
list we made to iterate over every single other player. However, if you want only 1 other player to be the runner, follow the instructions below:
local runner = players[math.random(1, #players)]
Now we can do the exact same thing we used to get the catcher, but for the runner instead. With all of this finalized, we now have both the players chosen, the catcher
and the runner
. Using these two players, you can do whatever you want with them, such as changing their walkspeed, giving them a specific item, etc.
The finalized code would look like this:
local players = game.Players:GetPlayers()
local catcher = players[math.random(1, #players)]
table.remove(players, table.find(players, catcher))
local runner = players[math.random(1, #players)] -- you can choose whether or not you want to include this line of code, depending on if you want every single other player to be the runner or only one