Giving a random player on a specific team a tool?

Hi. This is my first post on the DevForum, I am very excited to be in, but let’s get straight to it.

Its pretty simple. I want a random player on a specific team to get a tool when the function is called.

I did look around and couldn’t find anything.

Here is what I’ve tried that failed:

local player
local teams - game:GetService("Teams")
local function example()
player = teams.exampleTeam:GetPlayers()[math.random(1, #teams.exampleTeam:GetPlayers())]
game.ReplicatedStorage.ExampleTool:Clone().Parent = player

Any thoughts will help!

(This wasn’t copied and pasted so there may be some grammatical errors in it that weren’t there in studio. Thanks again!)

1 Like

You could use GetPlayers() to get the players on a team.

You could get the player like that :

`local randomTeam = game.Teams:GetChildren()[math.random(1,#game.Teams:GetChildren())]

local randomPlayer = randomTeam:GetPlayers()[math.random(1,#randomTeam:GetPlayers())]`

1 Like

No offense, but that’s what I did.

Other then the random team thing, because I don’t need a random team.

1 Like

I’d recommend something like this.

local teams = game:GetService("Teams")
local team = teams.exampleTeam:GetPlayers()
local randomPlayer = team[math.random(1,#team)]
print(randomPlayer) -- just for debugging

The variable randomPlayer will return the name of the player chosen, which you can then do whatever you need to do with it, which in this case would be copying the tool to the player’s Backpack.


My bad, I just saw that you wanted it on a specific team. You can do something like this:

local Teams = game:GetService("Teams")
local ChosenTeam = Teams:FindFirstChild("TeamName"):GetPlayers()

local randomPlayer = ChosenTeam[math.random(1,#ChosenTeam)]

Your issue stems from the fact that you’re parenting the cloned tool to the randomly selected player instance itself and not their backpack (where tools are typically stored).