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

Random player if they have a value?

Asked by 10 years ago

How would I make it so a script checks all the players, they have a BoolValue inside them, and if it's true, it randomly picks one of those players?

2 answers

Log in to vote
0
Answered by 10 years ago

Something along these lines:

local players = game.Players:GetChildren()
local chosen = {}

for num, obj in pairs(players) do
    local val = obj:FindFirstChild("BoolValue")
    if val and val.Value then
        table.insert(chosen, obj)
    end
end

local theChosenOne = chosen[math.random(#chosen)]
Ad
Log in to vote
0
Answered by
jav2612 180
10 years ago
PlayersSelection = {}
SelectedPlayer = nil

for i, v in pairs(game.Players:GetChildren()) do
    if v:FindFirstChild("BOOLVALUENAME") ~= nil then
        table.insert(PlayersSelection, v)
    end
end

if #PlayersSelection > 0 then
    rand = math.random(1, #PlayersSelection)
    SelectedPlayer = PlayersSelection[rand]
end

This will select a random player with that bool value and if there are no players with the bool value then it will just return nil.

Answer this question