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

ServerScriptService.Game:17: bad argument #2 to 'random' (interval is empty) ?

Asked by 9 years ago

This error is driving me CRAZY!!! I don't know what to do anymore to fix it!! This is the whole game script I have:

status = game.ServerScriptService.Status
local type = {}
leaderboard = nil
local w = game.Workspace:getChildren()
for i=1,#w do
    if w[i].Name == "leaderboard" and w[i]:findFirstChild("running") ~= nil and w[i]:findFirstChild("points") ~= nil then
        leaderboard = w[i]
    end
end

for i,game in pairs (game.ServerStorage:FindFirstChild("enemies"):GetChildren()) do
    table.insert(type,#type+1,type.Name)
end

while true do
    print (#type)
    typeName = type[math.random(1, #type)]
    typeName = game.ServerStorage.Maps:FindFirstChild(typeName)
    typeClone = typeName:Clone()    
    game.Workspace.Lobby:Play()
        for i = 60, 1, -1 do
            status.Value = "Intermission: "..i
            wait(1)
        end
        status.Value = "Disaster: "..typeClone.Name
        wait(3) 
        game.Workspace.running.Value = true 
        typeClone.Parent = game.Workspace.Parent
        typeClone:MakeJoints()
        for i = 150, 1, -1 do
            status.Value = "Time until round ends: "..i
            wait(1)
        end
        typeClone:remove()
        game.Workspace.running.Value = false    
        game.Workspace.VICTORY:Play()
        status.Value = "Congrats to whoever survived!"
        wait(3)
end


0
Please have a better desc. woodengop 1134 — 9y

2 answers

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

Your actual error is caused because math.random(1, #type)is invalid when '#type' is 0, which it is here, because lines 11, 12, and 13 are not populating the table. At all. This is happening because 'table.Name' is nil. I assume you mean to insert the Name of the 'enemy' you have currently selected in the loop?

for i, game in pairs (game.ServerStorage:FindFirstChild("enemies"):GetChildren()) do
    table.insert(type, game.Name) -- The position is not necessary here, and as I said above, `type.Name` is `nil`.
end
Ad
Log in to vote
0
Answered by 9 years ago

Computer Science protip: Never, ever, ever use a keyword for a variable name!

local status = game.ServerScriptService.Status;

local typeList = {};
local leaderBoard = nil;
local children = game.Workspace:getChildren();

for key, value in pairs(children) do
    if value.Name == "leaderboard" and value:findFirstChild("running") ~= nil and value:findFirstChild("points") ~= nil then
        local leaderBoard = value;

for i,game in pairs (game.ServerStorage.enemies:GetChildren()) do
    table.insert(typeList,#typeList+1,game.Name);
end

while true do
    print("The length is: " .. #typeList);
    typeName = typeList[math.random(1, #typeList)];
    typeName = game.ServerStorage.Maps:FindFirstChild(typeName);
    typeClone = typeName:Clone();
    game.Workspace.Lobby:Play();

    for i = 60, 1, -1 do
        status.Value = "Intermission: "..i;
        wait(1);
    end

    status.Value = "Disaster: "..typeClone.Name;

    wait(3);

    game.Workspace.running.Value = true;

    typeClone.Parent = game.Workspace;
    typeClone:MakeJoints();

    for i = 150, 1, -1 do
        status.Value = "Time until round ends: "..i;
        wait(1);
    end

    typeClone:remove();

    game.Workspace.running.Value = false  ; 
    game.Workspace.VICTORY:Play();

    status.Value = "Congrats to whoever survived!";

    wait(3);

end

Answer this question