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

Trying to pick a random player from server to become a hunter and getting confusing errors?

I need to pick a random player from the server and make them a hunter, but when I trigger this line of code I get an error like:

ServerScriptService.RoundSystem:106: invalid argument #2 to 'random' (interval is empty)

Here is the code:

local function teleportalltomap()
	wait_for_characters(players:GetPlayers()) -- call our function, passing all the players in the game
	print("All players' characters have loaded.")
	wait(2)
	local players = game.Players:GetChildren()
	local chosenhunter = players[math.random(1,#players)]
	print(chosenhunter)
	chosenhunter.Team = game.Teams.Hunters
	local propgun = game.ReplicatedStorage.PropGun:Clone()
	propgun.Parent = chosenhunter.Backpack
	huntercount.Value += 1
	chosenhunter.Character.HumanoidRootPart.CFrame = CFrame.new(maploc)

if someone can help me get rid of this error that would be awesome. Thanks!

3 comments

0

The error is at line 106, u r showing the script up to line 12.......

0

Which line is line 106 here?

0

Looks like the error is at line 6

1 answer

Hi, your code looks completely fine, However, the player will take some time to load, leaving #player = 0. Therefore the 2nd argument in math.random(1,#player) (which is #player) is 0, and it cannot choose any player because the 'interval' is empty.

So you want to make sure that either there is a player, meaning that you probably should not do the 'run' testing mode in studio and your wait should be a bit longer to ensure everyone has loaded.

Hope this helps!

Any questions? Just ask!

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