This is a support category for asking questions about how to get something done on the Roblox websites or how to do something on Roblox applications such as Roblox Studio.
You can write your topic however you want, but you need to answer these questions:
I want to create a system to choose a random player when there are at least 2 players in the game.
My issue is the script doesnt choose a random player and errors multiple times.
I’ve checked Youtube videos but most code outdated I checked the forum couldn’t find much on this particular topic.
So this is my script that is giving me difficulties if it is an easy fix if you could tell me why it was messed up I would appreciate it.
for _, plr in ipairs(game.Players:GetPlayers()) do
if #plr >= 2 then
local RandomPlayer = plr:GetChildren()[math.random(1,#plr)]
print(RandomPlayer)
end
end
if #plr >= 2 then should be checking the number of children of the player object, not the player object itself: Use the plr:GetChildren() instead.
for _, plr in ipairs(game.Players:GetPlayers()) do
local children = plr:GetChildren()
if #children >= 2 then
local randomPlayer = children[math.random(1,#children)]
print(randomPlayer)
end
end
you probably dont need the ipairs part, maybe thats the reason why?
you can use this:
if #game.Players:GetPlayers() >= 2 then
local RandomPlayer = game.Players:GetPlayers()[math.random(1,#game.Players:GetPlayers())]
print(RandomPlayer)
end
I didn’t try it out so tell me if anything bad happens