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

Why won't this work?

Here is the script that my friend gave me with a few edits:

minigames = game.Lighting.Minigames:GetChildren()
map = math.random(1, #minigames)

function Intermission()
	wait(2)
	for value = 35, 0, -1 do
		wait(1)
		game.Workspace.Timer.Value = "The game will start in "..value.." seconds."
	end
	wait(1)
	game.Workspace.Timer.Value = "Which map will we choose?"
	wait(3)
	game.Workspace.Timer.Value = "Map: "..map.Name
	return true
end

while true do
	wait(1)
	local Int = Intermission()
end

The problem is that line 13 does not work. Why?

1 answer

✓ Accepted answer

The problem.

No introductions, let's cut to the chase. map is a number. A random number between 1, and the number of minigames. not an instance. Lots of people make that mistake. If you were to return the map, it would return the random number between 1 and the # of maps. Why? because that's how math.random works. Only on numbers. Recieve 2 numbers, return one number. Not an instance, a number.

The Solution.

Now that we have the problem. let's find the solution. In tables (GetChildren returns a table) We can use table[number] to find that index (or number) in a table. So how would we apply this to this script? Glad you asked.

minigames = game.Lighting.Minigames:GetChildren() -- get the table of minigames
number = math.random(1,#minigames) -- define a number that's randomly chosen between 1 and the number of minigames
map = minigames[number] -- Iterate through the table of minigames to locate the number.

The Finished Script

minigames = game.Lighting.Minigames:GetChildren()
number = math.random(1, #minigames)
map = minigames[number]

function Intermission()
    wait(2)
    for value = 35, 0, -1 do
        wait(1)
        game.Workspace.Timer.Value = "The game will start in "..value.." seconds."
    end
    wait(1)
    game.Workspace.Timer.Value = "Which map will we choose?"
    wait(3)
    game.Workspace.Timer.Value = "Map: "..map.Name
    return true
end

while true do
    wait(1)
    local Int = Intermission()
end

  

1 comment

0

Good answer! You completely solved my problem!

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