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

invalid argument #2 to 'random' (interval is empty)?

Asked by 3 years ago
Edited 3 years ago

The Code is:

while true do

    local players = game.Players:GetChildren()



    local giant = players[math.random(1, #players)]

    wait()

    for i = 1,20 do

        local player = game.Players:GetChildren()
        status.Value = "Intermision "..20-i
        wait(1)
    end

    for i = 1,#players do
        if players[i].Character ~= nil then
            local spawnLocation = math.random(1,#workspace.Teleports:GetChildren())
            local giantspawnLocation = math.random(1,#workspace.GiantTeleport:GetChildren())
            players[i].Character:MoveTo(workspace.Teleports:GetChildren()[spawnLocation].Position)
            giant.Character:MoveTo(workspace.GiantTeleport:GetChildren()[giantspawnLocation].Position)
            PlayerSize(giant)

        end
    end
    for s = 1,20 do
        status.Value = "Game "..20-s
        wait(1)
        for l = 1,#players do

            giant.Character.HumanoidRootPart.Touched:Connect(function(player)
                local humanoid = players[l].Character.Humanoid
                if humanoid.Parent.Name ~= giant.Name then
                    humanoid:TakeDamage(100)
                end
            end)
        end
    end

    print(giant)

    wait(1)
    for p = 1,#players do


        giant.Character.Humanoid:TakeDamage(1000)
        players[p].Character.Humanoid.Health = 0
    end

end

0
is that all ur code is? if the variable players is 0, this error should happen. https://scriptinghelpers.org/lua#A4JwlgdgLgFAtgQygCwHQgRAJgezjARgBoAGASjKA greatneil80 2647 — 3y

1 answer

Log in to vote
0
Answered by
Benbebop 1049 Moderation Voter
3 years ago
Edited 3 years ago

Sometimes Roblox error codes are pretty unintuitive, this happens to be one of those cases.

invalid argument #2 to 'random' (interval is empty)

math.random spits out this error when the second value passed is smaller then the first.

print(math.random(1,5))
-- works fine

print(math.random(5,1))
-- causes error

My guess is that you are executing this code before any players have the chance to load. Thus making the players table empty and it registering as less then one.

Try delaying the code, using PlayerAdded or implementing a check for whether there are players in the server.

0
thank you! Zythicaal 2 — 3y
Ad

Answer this question