I will try to be as detailed as possible, so please bear with me
So basically, I have this presidential game with 3 parties, and I want 1 player to be picked randomly for each of the three teams. But for now, it is not random, and I pick a player by using .LocalPlayer in a localscript inside starterplayerscripts.
…
local Players = game:GetService(“Players”)
local player = Players.LocalPlayer
local Events = game.ReplicatedStorage.Events
local iAvailable = Events.IndependentAvailable
local dAvailable = Events.DemocratAvailable
local rAvailable = Events.RepublicanAvailable
local teamTaken = game.ReplicatedStorage.TeamTaken
local iTaken = teamTaken.IndependentTaken
local rTaken = teamTaken.RepublicanTaken
local dTaken = teamTaken.DemocraticTaken
player.Neutral = true
wait(10) --waits for a player to pick a team
while true do
wait(5)
if player.Team == game.Teams.Independent and iTaken.Value == false then
iAvailable:FireServer(player)
print("i available fired ")
wait(10)
end
end
…
I would like this to be random, with a percent chance of being picked on the screen somewhere. Is there any way to randomly pick a player out of each team?
First things first, I’d avoid doing this in a local script. This seems to be a critical task that is best left to the more secure server-side. Unless you want players to be able to manipulate which team they end up in, I recommend you do so.
With that out of the way, you can get a table of all the players using the GetPlayers method. I’d use the Random data type to generate a random number, use it to index a player from the table, assign them to a team, remove them from the table, and repeat until all players are distributed into the teams.
You can implement math.random to pick each player’s team. math | Documentation - Roblox Creator Hub
Example, lets say there is a 1/3 chance a player is picked for a certain team, you can use math.random(1, 3) and see if it selected a certain number like 1, 2, or 3.
Sorry, what I meant was I wanted a representative for each team each round., but randomly
So there would be 3 contestants who would debate for a win. An individual is drawn out from their team for a total of 3 different teamed individuals, one independent, one democratic, and one republican. (i want that part randomized, but I don’t know how to) The team part doesn’t matter, as each player gets to pick their team when they join.
**for some reason the team stuff only works in a local script, like player.neutral or checking what team a player is in
Sorry, what I meant was I wanted a representative for each team each round., but randomly.
So there would be 3 contestants who would debate for a win. The team part doesn’t really matter much, as the win is counted in a server script. I don’t know how to do that with math.random
local Teams = game:GetService("Teams")
local PARTIES = {
Teams.Independent,
Teams.Republican,
Teams.Democrat,
}
local function getRandomRepresentatives(): {Player}
local representatives = {}
for _, party in PARTIES do
local members = party:GetPlayers()
local member = members[math.random(#members)]
table.insert(representatives, member)
end
return representatives
end
The above code selects a random player from each party. The selection of a party member is impartial, so you can calculate your chance of being selected as 1 in server population
Since it’s a multiplayer game, the server should be the one to chose the representatives. If you’re unsure of when to involve the server, consider reading this article