How to get a random player every so often?

I am making a game in which random events occur in the environment or to the player, but for this I need the game to choose a random player every so often.

  1. What do you want to achieve? Have a random player chosen every 30 seconds (e.g.).

  2. What is the issue? Only one player is selected when executing the code and I don’t know how to implement it.

Here is my code:

local Players = game:GetService("Players")

if #Players:GetPlayers() < 1 then
	Players.PlayerAdded:Wait()
end

local RandomPlayer = Players:GetPlayers()[math.random(#Players:GetPlayers())]
print(RandomPlayer)

My code is a Script inside ServerScriptService

Any ideas on how to implement this?
Thanks in advance.

1 Like

just use a while wait for this, because it seems like you aren’t ever updating the random player.

while task.wait(30) do
	RandomPlayer = Players:GetPlayers()[math.random(#Players:GetPlayers())]
	print(RandomPlayer)
end
1 Like