Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What am I doing wrong?

This script is supposed to get 1 random player out of the players service and teleport him when he/ she is chosen. ERROR :bad argument #2 to 'random' (interval is empty)

players = game.Players:GetChildren()

while true do wait() playerchose = math.random(1,#players) playerchosen = players[playerchose] playerchosen.Character.Torso.CFrame = CFrame.new(Vector3.new(10,10,10)) wait(2) end end

1 comment

0

If an answer helped you, make sure to accept it.

1 answer

✓ Accepted answer

The issue is that the first argument is higher than the second argument. You're probably wondering how that's possible; the issue is that the script is loaded as soon as the game starts, when there are no players. That means #players is 0, so you're doing math.random(1,0), which means "get a random integer higher than 1 and less than 0", and there are no numbers like that!

What you need to be doing is something more like

while true do
	wait(2)
	local players=game.Players:GetPlayers()
	if #players>0 then
		local randomPlayer=players[math.random(#players)]
		--do stuff with randomPlayer
	end
end

1 comment

0

Thanks so much! @TheluaWeaver

This discussion is preserved as it was. New answers, comments, and votes are closed.