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

I dont understand this error: bad argument #2 (interval is empty) Can someone help?



local plrs = game.Teams.lobby:GetPlayers()


player = math.random(1,#plrs) 



print(player.Name)



2 answers

The error is telling you that there are no values inside of the plrs table. I believe this is because players are added during runtime while this script checks instantly (I'm assuming this is the only thing in your script). In order to fix this, you could either wait until a player joins by using a PlayerAdded event inside of a Server Script, or you could just wait for the character using a Local Script.

Server Script

game.Players.PlayerAdded:Connect(function(plr)
	local plrs = game.Teams.lobby:GetPlayers()
	local randomPlayer = plrs[math.random(1, #plrs)] 
	print(randomPlayer.Name)
end

Local Script

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait() --waits until the localplayer's character is loaded which is enough time for the player to join the team
local plrs = game.Teams.lobby:GetPlayers()
local randomPlayer = plrs[math.random(1, #plrs)] 
print(randomPlayer.Name)

2 comments

0

Thanks! It 100% works!

0

No problem! Please upvote and mark this question as the answer :)

First of all, try using the following format when writing code (it just clears things up a bit);

print("See, this is how code should be formatted on this site.")

Next, we are going to need quite a bit more information than you've given us. What are you trying to accomplish here? What is the result you hope for, and is anything happening other than that error? Please reply with more information (and maybe proper formatting) if you want an answer.

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