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

My script is giving an error? (Table)

Asked by
Relatch 550 Moderation Voter
9 years ago

I get an error when my script gets past the intermission. It doesn't pick the 2 players, and then throws a error. I'm pretty sure it gives this error because there is nothing in the table, but I have no idea of how to fix it. Help?

local matchTime = 45
local intermissionTime = 5
local playersNeeded = 2
local maps = game.ServerStorage["Maps"]:GetChildren()

_G["Game Active"] = false
_G["Match Time"] = 0
_G["Map"] = nil

function chooseMap(oldMap)
    local map 
    if not(oldMap == nil) then
        repeat map = maps[math.random(1, #maps)] until not(map == oldMap)
    else
        map = maps[math.random(1, #maps)]
    end
    return map
end

function choosePlayers(amount)
    local chosenPlayers = {}
    if amount >= #chosenPlayers then
        for i = 1, amount do
            local randomNum = math.random(1, #chosenPlayers)
            local player = chosenPlayers[randomNum]
            table.insert(chosenPlayers, #chosenPlayers + 1, player)
            table.remove(chosenPlayers, randomNum)
        end
        return chosenPlayers
    end
end

function reset(connections)
    _G["Game Active"] = false
    _G["Match Time"] = 0
    for i, v in pairs(game.Players:GetPlayers()) do
        pcall(function() v:LoadCharacter() end)
    end
    --for i, v in pairs(connections) do
    --    v:disconnect()
    --end
end

function match()
    local players = game.Players:GetPlayers()
    if #players >= playersNeeded then
        _G["Game Active"] = true
        _G["Match Time"] = matchTime
        --local connections = {}
        local fighters = choosePlayers(2)
        if not(_G["Map"] == nil) then
            _G["Map"] = chooseMap(_G["Map"])
        else
            _G["Map"] = chooseMap()
        end
        for i, v in pairs(fighters) do
            if v.Character == nil then
                repeat wait() until not(v.Character == nil)
            end
            v.Character:WaitForChild("Torso").CFrame = game.Workspace.Map1["Fighter Spawn"]..i
            local sword = game.ServerStorage.Sword:Clone()
            sword.Parent = v.Backpack
            local deathEvent = v.Character.Humanoid.Died:connect(function()
                table.remove(fighters, i)
                deathEvent:disconnect()
                if #fighters == 1 then
                    print(fighters[1].Name.. " won!")
                    reset()
                else
                    print(v.Name.." has been defeated!")
                end
            end)
            --table.insert(connections, #connections + 1, deathEvent)
            while _G["Match Time"] > 0 and wait(1) do
                _G["Match Time"] = _G["Match Time"] - 1
            end
            reset()
        end
    end
end

while wait(intermissionTime) do 
    local success, error = pcall(match)
    if not success then print(error) end
end
0
What does the error state? Spongocardo 1991 — 9y

Answer this question