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

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

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


1 comment

0

Please have a better desc.

2 answers

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

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

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