Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I select a random player in a team?

Asked by 3 years ago
Edited 3 years ago

I re-worded this, how would I be able to select a random player on a team and give them a tool?

2 answers

Log in to vote
0
Answered by 3 years ago

Solution

Using the method :GetPlayers() on a team will return a table of players in the team.

Script

local Teams = game:FindService("Teams")
local ServerStorage = game:FindService("ServerStorage")


local team = Teams.Team
local tool = ServerStorage.Tool


local function GiveRandomPlayerItem()
    local randPlayer = team:GetPlayers()[math.random(1,#team:GetPlayers())]

    tool:Clone().Parent = randPlayer.Backpack
end


GiveRandomPlayerItem()

Explanation

Finding the Teams and ServerStorage services.

A variable for the Team instance and a variable for the tool instance.

A function that will select a random player in the team and clones the tool and will set the parent to the specific player's backpack.

----------------------------------------------------------------------------

You can call the function anywhere in the Script at any time.

0
It tells me on line 10, "invalid argument #2 to 'random' (interval is empty)", I'm not a pro at stuff like this so I don't know what to do about it. Mystical_Un1verse 9 — 3y
Ad
Log in to vote
0
Answered by 2 years ago

In the end, I made a script which gave the player on the team a tool and a GUI that can only be seen by the player.

Answer this question