Hi Guys, so I got to think that can we save a custom model using tables? Like for example, in a game Custon PC Tycoon, we can make a custom pc using computer parts i.e. models provided by the developer. So we can design our own pc using this and it saves also. And if you have worked with tables, which I think you must be, then you can observe that you could add instances i.e. objects as well in tables. So my idea is to when player made the pc i.e. the model, why not add the instance in a table, and when the player leaves, just load it ? Is it possible, please respond and help me out. Thanks
You can’t store Instances in a data store. Instead, store enough information to reconstruct the necessary instances. E.g. if you have a specific type of model in a specific CFrame, just give a number ID to each model type, then store that ID and the CFrame.
I don’t want to store instances but store a table and store instances in it. But I want to also know that if we add an instance inside a table, does it adds an instance or just it’s name ? Because if I added a part as instance let’s say, and called it from the table, I was able to run all the functions in a part and see all the properties of it.
If I understand what you are asking you want to store the parts of a players pc in a table of data for each individual player so that when they rejoin they would rebuild it automatically. To do this to make your life easier instead of when the player leaves simply table.insert(part.Name) when the part is created or table.Remove(PartIndex) - [or you can loop through the table until you find the specific part and remove it].
When using data store it has to be in a Server Script. I recommend putting this in “ServerScriptService”
This will be a server script to hold your values.
--Services
local DSS = game:GetService("DataStoreService")
--Data Stores
local PlayerDataDS = DSS:GetDataStore("PlayerDataDS")
local t = {} -- your table of parts
local folder = Instance.new("Folder")
folder.Parent = game.ServerStorage --ideally make this folder in your storage with all existing parts in rep storage without this code.
game.Players.PlayerAdded:Connect(function(player)
-- this is just a place for your parts to go when created
-- so we can grab em later
local IngameFolder = Instance.new("Folder") -- add your parts here
IngameFolder.Name = "Player1PartsFolder" -- can set the to player.Name or Id so people don't have the same folder names.
IngameFolder.Parent = workspace
for i, v in pairs(PlayerDataDS:GetAsync(player.UserId)) do
local ingamePart = folder:WaitForChild(v):Clone()
ingamePart.Parent = game.workspace:WaitForChild(IngameFolder) -- can set this to wherever you want
end
end)
game.Players.PlayerRemoving:Connect(function(player)
for i, v in pairs(game.workspace.Player1PartsFolder:GetChildren()) -- can do GetDescendents if you want the children of children included too.
if v:IsA("Part") then -- checks if the found child is a part
table.insert(t,v.Name)
end
end
PlayerDataDS:SetAsync(player.UserId, t) -- saves all part names to the player so when they rejoin it will be loaded.
print("Saved Player Data: ", PlayerDataDS:GetAsync(player.UserId))
end)
-- can optionally use pcall to check and make sure everything saved the way it should.
To store the position of the parts do the exact same thing as the data store we did for the parts name except for this time you store the part.Position or make the table store 2 tables being the parts name and parts position [1][1] and make sure their positions in the tables are the same but you most likely won’t want this and will want to set it to the players specific platform location given as its a tycoon.
No that would not be possible because Instances are unable to be saved to data store as data store just holds information and not physical instances. The way to go around this is by saving the part names and finding those parts which are stored in your storage and cloning them to wherever you want as the code I provided does.