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?
COMMUNITY QUESTION
Random player if they have a value?
2 answers
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)]
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.
This discussion is preserved as it was. New answers, comments, and votes are closed.
