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

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

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?

2 comments

0

You forgot an 'end'

0

This wouldn't with with the end too

1 answer

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!

1 comment

0

Very Interesting.. These are all possibilities. Thanks!

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