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

Why doesn't table.insert/remove work in my case?

Asked by 6 years ago
Edited 6 years ago

Let me just show you my script before I ask:

local debounce = true
local ExampleTable = {} -- open for players names to be put in

script.Parent.Touched:Connect(function(player)
    if not debounce then 
        return
    end
    debounce = false

    if not ExampleTable[player.Parent.Name] then
        print("Player is not in table")
        table.insert(ExampleTable, player.Parent.Name)
        wait(1)
        debounce = true
    else
        print("Player's name is in table")
        table.remove(ExampleTable, player.Parent.Name)
        wait(1)
        debounce = true
end)

Everytime I touch the brick, it tells me over and over that it is not in the table.

Is my table.insert is wrong or what? Should I even use this?

0
You forgot an 'end' User#20388 0 — 6y
0
This wouldn't with with the end too Leamir 3138 — 6y

1 answer

Log in to vote
0
Answered by
Leamir 3138 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

To test if a argument is on a Table, you must use TABLE[1 ~ #TABLE] Change the "1 ~ #TABLE" to a number(of the argument you want to get)

Example:

TestTable = {"Leamir", "Is A Noob"}

print(TestTable[2]) -- This will print "Is A Noob"

But, if you want to test if a name is on the table? Its eazy, you must use a For Loop!

Example:

TestTable = {"Leamir", "Is A Noob"}

for i,v in pairs(TestTable) do
    print(v) -- This will print "Leamir" and the "Is A Noob"
    if v == "Leamir" then -- This will test if table has "Leamir", you can use a player name!
        OnTable = true
    end
    if OnTable = true then
        print(v .. " is on the table!")
        OnTable = false
    else
        print(v .. "isn't on the table")
    end
end

I also made a script to test if a random player is on the table, if not add it!

local Players = game.Players:GetChildren()
local Table = {}

for i,v in pairs(Table) do
    if v == Players[math.Random(1, #Players)] then -- This will test if table has a random player name!
        OnTable = true
    end
    if OnTable = true then
        print(v .. " is on the table!")
        OnTable = false
    else
        print(v .. "isn't on the table")
        table.Insert(Table, v)
    end
end

Helpful articles:

table.remove

Table Array

Good Luck with your games!

0
Very Interesting.. These are all possibilities. Thanks! MusicalDisplay 173 — 6y
Ad

Answer this question