Choosing a random player does not work

local players = game.Players:GetPlayers()
			
			local evilyozy = table.find(players, game.Players:FindFirstChild("EvilYozy"))
			if evilyozy then
				table.remove(players, evilyozy)
			end
			
			if #players < 1 then
				local randomplayer = players[math.random(1, #players)]
				if randomplayer.Character and randomplayer.Character:FindFirstChild("Head") then
					-- some other code
				end
			else
				print("Positional Laser: No players were found!")
			end

The code is supposed to choose a random player and run code with that player but it can’t choose a player named “EvilYozy”. If there are no players in the game other than EvilYozy then it will print “Positional Laser: No players were found!” However, when no players are found, it just says “invalid argument #2 to ‘random’ (interval is empty)” and doesn’t work. How do I make it so that the print statement happens if only EvilYozy is in the game but the code will fire for a random player if there is at least one other player in the game?

1 Like
if #players < 1 then

Why are you trying to pick a random player only when there are 0 or less players? :wink:

3 Likes

Two issues:

  1. Reverse condition (it must be #players > 0)
  2. You have changed your username, so you won’t be automatically removed from the available options. That’s why we use user ids to identify users.
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.