Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago

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!

0
The error is at line 106, u r showing the script up to line 12....... sne_123456 439 — 3y
0
Which line is line 106 here? AlexanderYar 788 — 3y
0
Looks like the error is at line 6 sne_123456 439 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

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!

Ad

Answer this question