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

Minigame Script error, "attempt to index a number value" when randomizing map choice?

Asked by 7 years ago

I'm making a minigame script and it's pretty long... But when the script randomizes the map before loading it, an error shows up in the output: "attempt to index a number value". i'm sorry if the script is too long, i'm not sure if the rest of the script is irrelevant or not.

local maps = game.ServerStorage:WaitForChild("Maps"):GetChildren()
local lobby = game.Workspace:WaitForChild("Lobby")
local message = game.Workspace:WaitForChild("Message")
local numplayers = game.Workspace:WaitForChild("NumPlayers")
local playersNeeded = 1 --Temporarily for testing
local chosenmap = game.Workspace:WaitForChild("ChooseMap")
local selected = game.Workspace:WaitForChild("SelectedMap")


function deleteLastMap()
    selected:ClearAllChildren()
end

function checkPlayers()
    if numplayers.Value >= playersNeeded then
        return true
    else
        return false
    end
end

function getAlivePlayers()
    local n = 0
    local players = game.Players:GetPlayers()
    for i=1, #players do
    if players[i].Settings.afk == false then
        if players[i].Character ~= nil then
            if players[i].Character.Humanoid.Health ~= 0 then
                if players[i].Settings.Playing.Value ~= false then
                    n = n+1
                end
            end
        end
    end
    return n
    end
end

function getWinners()
    local n = 0
    local players = game.Players:GetPlayers()
    for i=1, #players do
    if players[i].Settings.afk == false then
        if players[i].Character ~= nil then
            if players[i].Character.Humanoid.Health ~= 0 then
                if players[i].Settings.Winner.Value ~= false then
                    n = n+1
                end
            end
        end
    end
    return n
    end
end

function intermission()
    for intermissiontime = 5, 1 do -- in seconds
        wait(1)
        message.Value = "||"..intermissiontime.."||"
    end
end

function choosemap() 
    message.Value = "Choosing minigame..."
    chosenmap.Value = math.random(1, #maps).Name -- where the error is 
end

function displayChosen()
    message.Value = "Minigame chosen:"..chosenmap.Value.."!"
end

function loadMinigame()
    message.Value = "Loading map..."
    local clone = game:GetService("ServerStorage").Maps:FindFirstChild(chosenmap.Value)
    clone:Clone().Parent = workspace
end

function startGame()
end

while true do
    wait()
if checkPlayers() then
        intermission()
        wait (1)
        choosemap()
        wait(3)
        displayChosen()
        wait(2)
        loadMinigame()
        wait(2)

    end
end

1 answer

Log in to vote
1
Answered by
Link150 1355 Badge of Merit Moderation Voter
7 years ago
Edited 7 years ago

The "attempt to index X" error message means you have tried to get something from X using the index operator, a[b] or a.b.

In your particular case, you attempted to get something out of a number value. This obviously makes no sense to Lua, and thus it errors.

Here's the problem. math.random simply generates a random number. It can't pick a random element from your map list for you. However, what you can do is use the number returned as a position in your table:

local r = math.random(#maps)
local map = maps[r]

print(map.Name)
0
Thanks! That helped a lot! VeryRaven 85 — 7y
0
No problem. :) Link150 1355 — 7y
Ad

Answer this question