How would I fix this error?

Sorry for being vague in the title, don’t really know how to word that, anyway.

I want to make a script that picks a random player. That’s all. I tried other questions like this, but it has one error:

ServerScriptService.Script:3: invalid argument #2 to ‘random’ (interval is empty)

How would I fix this? Heres the script:

local function PickRandom(part)
	local players = game:GetService("Players"):GetPlayers()
	local ranplr = players[math.random(1,#players)].Character
	if ranplr and ranplr.HumanoidRootPart then
		print("hi")
	end
end

PickRandom()

This is a serverscript inside of ServerScriptService

This is most likely because there is only 1 player in the game so #players would return 1. This would mean math.random(1,1) which is an empty interval

1 Like

I think it runs the script before the player is fully loaded.

Try this:

local function PickRandom(part)
	local players = game:GetService("Players"):GetPlayers()
	local ranplr = players[math.random(1,#players)].Character
	if ranplr and ranplr.HumanoidRootPart then
		print("hi")
	end
end

wait(5)

PickRandom()

This still works, I just tested it.

2 Likes