How can I pick a random player from a team?

Hello!
How can I pick a random player from a team?
Script:

while true do
	local Status = game.ReplicatedStorage:WaitForChild("Status")
	local BCG = game.ReplicatedStorage:WaitForChild("BrickColorGui")
	repeat
		wait(0.01)
		local playerTable = #game.Players:GetPlayers()
		game.StarterGui.TalkGui.TalkFrame.TalkText.Text = "Waiting for 8 players... There are "..playerTable.." players in this server!"
	until
	playerTable >= 1

	local playerTable = #game.Players:GetPlayers()

	if playerTable >= 1 then
		Status.Value = "Starting..." -- change the values to what you want it to say
		wait(3)
		Status.Value = "Welcome to Endurance v2!"
		wait(4)
		Status.Value = "This is a game based off the original Endurance game made by Endurance ©."
		wait(4)
		Status.Value = "The rules of playing this game are very simple."
		wait(3.5)
		Status.Value = "You have to choose your brick color, race on challenges, Random Players except winner will be picked. Fight to death."
		wait(10)
		Status.Value = "This process repeats until 2 players remain! They fight to death until they run out of bricks."
		wait(8)
		Status.Value = "You may choose your brick color."
		wait(4)
		BCG.Value = true
		wait(10)
		BCG.Value = false
		Status.Value = "We may now start this round of Endurance v2, Good Luck!"
		local interVal = 20
		repeat wait(1)
			interVal -= 1
			Status.Value = "Intermission: "..interVal
		until
		interVal <= 0
		local maps = game.Lighting.Maps:GetChildren()
		local randomMap = maps[math.random(1, #maps)]
		local map = randomMap:Clone()
		map.Parent = workspace
		wait(2)
		local randomBlockWorth = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
		local BlockWorth = randomBlockWorth[math.random(1, #randomBlockWorth)]
		Status.Value = "This minigame is worth "..BlockWorth.." blocks!"
		wait(4)
		Status.Value = "This minigame we are playing today is..."
		wait(4)
		Status.Value = map.Name.."!"
		game.ReplicatedStorage:WaitForChild("Minigame").Value = map.Name
		wait(3)
		Status.Value = "Good luck!"
		wait(3)
		local players = game.Players:GetPlayers()
		for i,v in pairs(players) do -- teleport all the players inside the playing team since we removed all the ones who aren't in the playing team
			if v:IsA("Player") then
				if v.Team == game.Teams:WaitForChild("Playing") then
					local Char = v.Character
					v.Team = game.Teams.Alive
					Char:PivotTo(map:WaitForChild("TeleportToMinigame").CFrame) -- put the spawn's CFrame between the pivotTo's brackets.
				end
			end

		end
		wait(3)
		if game.ReplicatedStorage:WaitForChild("Minigame").Value == "Last to Leave Island" then
			Status.Value = "Welcome to Last to Leave Island!"
			wait(4)
			Status.Value = "In this minigame, you have to fight on an island with your sword."
			wait(6)
			Status.Value = "Fight your opponent and the loser will be eliminated."
			wait(5)
			Status.Value = "The winner will have to fight the next winner."
			wait(4)
			Status.Value = "Earthworm Sally/Mayor of the Island will pick the random players."
			wait(8)
			Status.Value = "Good Luck!"
			wait(4)
			Status.Value = "Mayor: I am the great Mayor of this island!"
			wait(5)
			Status.Value = "Mayor: I choose to pick the following to fight..."
			wait(5)
			local playerTableforAliveTeam = #game.Teams.Alive:GetPlayers()
			Status.Value = "Mayor: "..playerTableforAliveTeam[math.random(1, #playerTableforAliveTeam)].." VS..."
			wait(5)
			Status.Value = "Mayor: "..playerTableforAliveTeam[math.random(1, #playerTableforAliveTeam)].."!"
			wait(5)
		end
	end

end

I need help. Thanks!

1 Like

This part errors:

local playerTableforAliveTeam = #game.Teams.Alive:GetPlayers()
Status.Value = "Mayor: "..playerTableforAliveTeam[math.random(1, #playerTableforAliveTeam)].." VS..."
wait(5)
Status.Value = "Mayor: "..playerTableforAliveTeam[math.random(1, #playerTableforAliveTeam)].."!"

# operator returns the table’s length. To index one of its values you should use the table itself, not its length.

local playerTableforAliveTeam = game.Teams.Alive:GetPlayers()
Status.Value = "Mayor: "..playerTableforAliveTeam[math.random(1, #playerTableforAliveTeam)].." VS..."
task.wait(5)
Status.Value = "Mayor: "..playerTableforAliveTeam[math.random(1, #playerTableforAliveTeam)].."!"
2 Likes

Where should I fix the error then?

What’s the error you’re receiving?

local function getRandomPlayerFromTeam(team: Team)
    if #team:GetPlayers() == 0 then return end
    return team:GetPlayers()[math.random(#team:GetPlayers())]
end
local randomAliveTeamPlayer = getRandomPlayerFromTeam(game.Teams.Alive)
print(randomAliveTeamPlayer.Name)

ServerScriptService.Script:84: attempt to get length of a number value

Here’s the solution.

? Thats just my own code its not the solution

I linked to the wrong reply, apologies.

It errored again: ServerScriptService.Script:84: attempt to concatenate Instance with string

local playerTableforAliveTeam = game.Teams.Alive:GetPlayers()
Status.Value = "Mayor: "..playerTableforAliveTeam[math.random(1, #playerTableforAliveTeam)].Name.." VS..."
task.wait(5)
Status.Value = "Mayor: "..playerTableforAliveTeam[math.random(1, #playerTableforAliveTeam)].Name.."!"

They just forgot to index the “Name” property of the randomly select player instance.

I’d also advise adding a check to make sure the length of the array is greater than 0 (so that the team has at least one player).

local playerTableforAliveTeam = game.Teams.Alive:GetPlayers()
if #playerTableforAliveTeam > 0 then
	Status.Value = "Mayor: "..playerTableforAliveTeam[math.random(1, #playerTableforAliveTeam)].Name.." VS..."
	task.wait(5)
	Status.Value = "Mayor: "..playerTableforAliveTeam[math.random(1, #playerTableforAliveTeam)].Name.."!"
end

Whats task.wait? I’ve never heard of that before.

Oh, I didn’t add that to the script but task.wait() is just a newer version of wait().

You just earned yourself a check-mark solution! Tysvm…

1 Like