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

Why won't the table insert a value?

Asked by 5 years ago

Hello, I am currently having trouble on inserting a value. Every time I do it, it gives me an error. If you know anything about this error or what it means and how I can possibly fix it, please let me know. Thanks! (And also, if you need any more information, I'd be glad to get you what you need.)

Error:

Workspace.GameScripts.Maps.FoggyRailroad:102: attempt to index upvalue 'table' (a nil value)

Workspace.GameScripts.Maps.FoggyRailroad scripts:

local table spikestween = {
    [1] = tweenspike1,
    [2] = tweenspike2,
    [3] = tweenspike3,
    [4] = tweenspike4,
    [5] = tweenspike5,
    [6] = tweenspike6,
    [7] = tweenspike7,
    [8] = tweenspike8,
    [9] = tweenspike9,
    [10] = tweenspike10,
    [11] = tweenspike11,
    [12] = tweenspike12,
}


local table avoidings = {
    [1] = "Train",
    [2] = "Zombies",
    [3] = "Spikes",
    [4] = "Brawl",
    [5] = "Bombs",
    [6] = "Lightning",
    [7] = "Tag",
    [8] = "Coin Multiplier",
    [9] = "Disco Players",
    [10] = "Gravity Coil",
    [11] = "Speed Coil",
    [12] = "Rocket Brawl",
    [13] = "Bomb Brawl",
    [14] = "Character Jam",
    [15] = "Murderer"
}

local table alreadyused = {}  -- THIS IS THE TABLE USED
local avoiding = nil

game.ReplicatedStorage.CurrentMap.Changed:Connect(function()
    if rs.CurrentMap.Value == "Foggy Railroad" then
        rs.Foggy_Railroad.AvoidAdded:Fire("Trains")
        repeat
            avoiding = avoidings[5]
            if avoiding == "Zombies" then
                table.insert(alreadyused, "Zombies")
                for i = 5, 0, -1 do
                    local clone = game.ServerStorage.Zombie:Clone()
                    gfunction.teleportAnything(clone, game.Workspace.Railroad_Spawn)
                    clone.Parent = workspace
                end
            else if avoiding == "Spikes" then
                table.insert(alreadyused, "Spikes")
                for i = 12, 1, -1 do
                    spikestween[i]:Play()
                end

            else if avoiding == "Brawl" then
                table.insert(alreadyused, "Brawl")
                for i,p in ipairs(game.Players:GetPlayers()) do
                    gfunction.giveGear(p, game.ServerStorage.ClassicSword)
                end
            else if avoiding == "Bombs" then
                table.insert(alreadyused, "Bombs") -- THIS IS THE LINE THAT GOT THE ERROR
                rs.Foggy_Railroad.AvoidAdded:Fire("Bombs")
            else if avoiding == "Lightning" then
                table.insert(alreadyused, "Lightning")
                rs.Foggy_Railroad.AvoidAdded:Fire("Lightning")
            else if avoiding == "Tag" then
                table.insert(alreadyused, "Tag")
                -- Do Later
            else if avoiding == "Coin Multiplier" then
                table.insert(alreadyused, "Coin Multiplier")
            else if avoiding == "Disco Players" then
                table.insert(alreadyused, "Disco Players")
                rs.Foggy_Railroad.AvoidAdded:Fire("Disco Players")
            else if avoiding == "Gravity Coil" then
                table.insert(alreadyused, "Gravity Coil")
                for i,p in ipairs(game.Players:GetPlayers()) do
                    gfunction.giveGear(p, game.ServerStorage.GravityCoil)
                end
            else if avoiding == "Speed Coil" then
                table.insert(alreadyused, "Speed Coil")
                for i,p in ipairs(game.Players:GetPlayers()) do
                    gfunction.giveGear(p, game.ServerStorage.SpeedCoil)
                end
            else if avoiding == "Rocket Brawl" then
                table.insert(alreadyused, "Rocket Brawl")
                for i,p in ipairs(game.Players:GetPlayers()) do
                    gfunction.giveGear(p, game.ServerStorage.RocketLauncher)
                end
            else if avoiding == "Bomb Brawl" then
                table.insert(alreadyused, "Bomb Brawl")
                for i,p in ipairs(game.Players:GetPlayers()) do
                    gfunction.giveGear(p, game.ServerStorage.Bomb)
                end
            else if avoiding == "Character Jam" then
                table.insert(alreadyused, "Character Jam")
                rs.Foggy_Railroad.AvoidAdded:Fire("Character Jam")
            else if avoiding == "Murderer" then
                local randomplayer = game.Players:GetPlayers()[math.random(1,#game.Players:GetPlayers())]

                gfunction.giveGear(randomplayer, game.ServerStorage.Handgun)
            end
            end
            end
            end
            end
            end
            end
            end
            end
            end
            end
            end
            end
            end
            wait(15)
        until game.ReplicatedStorage.CurrentMap ~= "Foggy Railroad"
        end
end)
0
First of all, variables are lexical(I think that's what you call it). This means that the variable's type is from its value.Thus, making it so you don't need to type in i.e          local int myVar = 5 (You also don't have to type in constant,static etc.). You don't need to make a new dictionary as a number. You can do tbl[index]. Third of all, you should be using elseif instead of else if to decr saSlol2436 716 — 5y
0
I am extremely confused. I don't know how that is suppose to help the issue I'm having. ZenerFox 0 — 5y
0
The variables part say what is incorrect about the code. Since you have a space in your variable name, it makes that variable nil. saSlol2436 716 — 5y
0
That isn't a space, "alreadyused" is the variable name. table is the variable type. I don't know if that has to do anything with the error though. ZenerFox 0 — 5y
View all comments (4 more)
0
I would like to chat with you in the scripting helpers chat and help you :) saSlol2436 716 — 5y
0
How do I get there? I am kind of a new guy to scripting helpers. ZenerFox 0 — 5y
0
@ZenerFox, I said that you don't need to specify the type of the variable in Lua. What is actually happening is that it will get the variable's value, and from that, it will say its type saSlol2436 716 — 5y
0
@ZenerFox see a bar saying, "Community Chat"? saSlol2436 716 — 5y

1 answer

Log in to vote
0
Answered by
RAYAN1565 691 Moderation Voter
5 years ago
Edited 5 years ago
local spikestween = {"tweenspike1", "tweenspike2", "tweenspike3", "tweenspike4", "tweenspike5", "tweenspike6", "tweenspike7", "tweenspike8", "tweenspike9", "tweenspike10", "tweenspike11", "tweenspike12"}


local avoidings = {"Train", "Zombies", "Spikes", "Brawl", "Bombs", "Lightning", "Tag", "Coin Multiplier", "Disco Players", "Gravity Coil", "Speed Coil", "Rocket Brawl", "Bomb Brawl", "Character Jam", "Murderer"}

local alreadyused = {}  -- THIS IS THE TABLE USED
local avoiding = nil

game.ReplicatedStorage.CurrentMap.Changed:Connect(function()
    if rs.CurrentMap.Value == "Foggy Railroad" then
        rs.Foggy_Railroad.AvoidAdded:Fire("Trains")
        repeat
            avoiding = avoidings[5]
            if avoiding == "Zombies" then
                table.insert(alreadyused, "Zombies")
                for i = 5, 0, -1 do
                    local clone = game.ServerStorage.Zombie:Clone()
                    gfunction.teleportAnything(clone, game.Workspace.Railroad_Spawn)
                    clone.Parent = workspace
                end
            else if avoiding == "Spikes" then
                table.insert(alreadyused, "Spikes")
                for i = 12, 1, -1 do
                    spikestween[i]:Play()
                end

            else if avoiding == "Brawl" then
                table.insert(alreadyused, "Brawl")
                for i,p in ipairs(game.Players:GetPlayers()) do
                    gfunction.giveGear(p, game.ServerStorage.ClassicSword)
                end
            else if avoiding == "Bombs" then
                table.insert(alreadyused, "Bombs") -- THIS IS THE LINE THAT GOT THE ERROR
                rs.Foggy_Railroad.AvoidAdded:Fire("Bombs")
            else if avoiding == "Lightning" then
                table.insert(alreadyused, "Lightning")
                rs.Foggy_Railroad.AvoidAdded:Fire("Lightning")
            else if avoiding == "Tag" then
                table.insert(alreadyused, "Tag")
                -- Do Later
            else if avoiding == "Coin Multiplier" then
                table.insert(alreadyused, "Coin Multiplier")
            else if avoiding == "Disco Players" then
                table.insert(alreadyused, "Disco Players")
                rs.Foggy_Railroad.AvoidAdded:Fire("Disco Players")
            else if avoiding == "Gravity Coil" then
                table.insert(alreadyused, "Gravity Coil")
                for i,p in ipairs(game.Players:GetPlayers()) do
                    gfunction.giveGear(p, game.ServerStorage.GravityCoil)
                end
            else if avoiding == "Speed Coil" then
                table.insert(alreadyused, "Speed Coil")
                for i,p in ipairs(game.Players:GetPlayers()) do
                    gfunction.giveGear(p, game.ServerStorage.SpeedCoil)
                end
            else if avoiding == "Rocket Brawl" then
                table.insert(alreadyused, "Rocket Brawl")
                for i,p in ipairs(game.Players:GetPlayers()) do
                    gfunction.giveGear(p, game.ServerStorage.RocketLauncher)
                end
            else if avoiding == "Bomb Brawl" then
                table.insert(alreadyused, "Bomb Brawl")
                for i,p in ipairs(game.Players:GetPlayers()) do
                    gfunction.giveGear(p, game.ServerStorage.Bomb)
                end
            else if avoiding == "Character Jam" then
                table.insert(alreadyused, "Character Jam")
                rs.Foggy_Railroad.AvoidAdded:Fire("Character Jam")
            else if avoiding == "Murderer" then
                local randomplayer = game.Players:GetPlayers()[math.random(1,#game.Players:GetPlayers())]

                gfunction.giveGear(randomplayer, game.ServerStorage.Handgun)
            end
            end
            end
            end
            end
            end
            end
            end
            end
            end
            end
            end
            end
            end
            wait(15)
        until game.ReplicatedStorage.CurrentMap ~= "Foggy Railroad"
        end
end)

Try it now. The way you written your tables was incorrect. It should be written the way I did them. In addition, you do not need to define that you're using a table by doing this:

local table a = {} --THIS IS WRONG!

This is correct:

local a = {}

The { } lets it know you're making a table.

Also, I do not know if this is part of a larger script but make sure that rs and gfunction is correctly defined within the script.

0
FYI, all I changed was lines 1-6. RAYAN1565 691 — 5y
Ad

Answer this question