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!
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!