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
Oh, then just use table.find to find the position of that value.
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.
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
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…
Uhh… I get it. But then why I was serializing all the data of a part for saving it… Thanks for opening my eyes.
Edit - How to save model of parts using tables(Data store) - #2 by w4570
Even this person says that parts can’t be directly stored… I’m confused.
Then why serialization exists if we could directly save a part inside a table…
Parts stored in the table are references to parts that exist in memory during runtime. Think of them as unique IDs to find the appropriate objects anywhere in game (and also outside the data model).
Our own serialization, however, has to be done when storing properties of parts in the data store. The data is going to persist long after the server closes, engine references and memory addresses will be different in a new game instance. We are going to create a new part with a new ID and apply stored properties.
Furthermore, serialization allows us to compress the data and save as much memory as we can to comply with data store size limits.
Hopefully this helps.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.