How can I find this value in table and remove it?

So right now i have this script

local characters = {}

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		table.insert(characters, char)
	end)
	player.CharacterRemoving:Connect(function(char)
		table.remove(characters, char)
	end)
end)

It works fine until I try to remove the character from the table when the character dies. How can I fix this to find the position of the character then remove it from the table?

3 Likes

Docs are pretty good for thi:

local index = table.find(yourtable, searchitem)
table.remove(yourtable, index)
7 Likes

Char is actually an object I guess. You can’t put object in tables.

Do this -

local characters = {}

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		table.insert(characters, char.HumanoidRootPart.Position)
	end)
	player.CharacterRemoving:Connect(function(char)
		table.remove(characters, table.find(characters, char.HumanoidRootPart.Position))
	end)
end)
1 Like

In table.remove, the second parameter is the position of what you want to remove. So if you use table.find like this: table.remove(characters,table.find(characters,char)) it should work.

There’s probably better ways to do it, but this is what I would do.

1 Like

You actually can put an object in a table since script functioned as intended but I just had to know how to find it’s position in the table which I didn’t know how, Your script would also not work

1 Like

Oh, then just use table.find to find the position of that value.

1 Like

As others have pointed out, table.remove requires an index. The reason why is that you could have multiple of the same element in a table, so table.remove wouldn’t know which one to remove. We can get the index by using table.find, which returns the index of the first element that it’s searching for.

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		table.insert(characters, char)
	end)
	player.CharacterRemoving:Connect(function(char)
		table.remove(characters, table.find(characters, char))
	end)
end)

Hope this helped!

You absolutely can put objects in tables. This is the basis of Object Oriented Programming, or OOP.

You can just do this:

local characters = {}

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		table.insert(characters, char)
	end)
	player.CharacterRemoving:Connect(function(char)
		table.remove(characters, table.find(characters, char))
	end)
end)

Or in another way you could chek all the objects in the table cheking if the object is equal to the character and if it is break the loop and remove the index.

I meant you can’t put it directly. You need to add every property of a part as a table if using OOP.

No you don’t. You can just add the character object straight to the table.

I don’t think we can add parts directly to tables. I also wonder what will it print if we do that? When I do that, it gives an error.

Trust me, you can. It’ll print the name of the object. You probably got an error because you did it incorrectly.

I did something like this -

local part = workspace.Part --assume
local tab = {}

table.insert(tab, part)
print(table[1])

Yep, this’ll work.
Think about the :GetChildren() method, which returns a table of objects.

1 Like

So basically when I insert a part, it is in the form of a table which has all the properties of a part?

This thread become pretty long. @Wigglyaa already gave the answer at the very top.

@Moneypro456789 objects can be stored in tables just like into variables. Instances are userdata, and they are referenced with a special Roblox’s reference ID. Similarly, printing tables in log mode displays their memory address. Tables are objects too.

No.
When you insert a part into the table, you can reference the part through the table.
For example:

local part = workspace.Part
local partTable = {}
table.insert(partTable, part)
print(partTable.part.Position)

This will return Part’s position

1 Like

Yeah, that’s what I wanted to ask that it is in the form of table which has all the properties like it’s position, color, orientation etc.

It’s not really “in the form of a table”… just accessible in the table…