Ad
Log in to vote
0

How do I select a random player in a team?

Asked by 20 days ago
Edited 20 days ago

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

1 answer

Log in to vote
0
Answered by 20 days 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 0 — 20d
Ad

Answer this question