I’m using a dictionary system to store one value, the velocity of a part and I’m wondering if what I’ve made is an efficient system.
What can be changed about the code below?
local parts = {}
for _, v in pairs(workspace:GetDescendants()) do
if v:IsA("BasePart") and not v.Anchored then
table.insert(parts, v)
if v.Velocity ~= Vector3.new(0,0,0) then
parts[v] = {Velocity = v.Velocity} -- how can I check if a model was here and only save the primarypart's velocity?
else
parts[v] = nil
end
v.Anchored = true
v.Velocity = Vector3.new(0,0,0)
end
end
for _, part in pairs(parts) do
if parts[part] then
part.Velocity = parts[part].Velocity
end
part.Anchored = false
end
This code saves everything in one dictionary, with sub-dictionaries. Each model will be saved as a dictionary with a Velocity and all the model’s children. Each part is also saved as a dictionary with all the properties you want to save. I only made the Velocity save if the part’s parent is the Workspace.
Code with recursion:
local parts = {}
function getParts(model)
local parts = {}
for i, p in ipairs(model:GetChildren()) do
if p:IsA("BasePart") then
parts[i] = {} --you can replace parts[i] with parts[p.Name] if p.Name is unique
parts[i].Position = p.Position
if model.Parent == game.Workspace then
parts[i].Velocity = p.Velocity
end
--add more properties if you want
elseif p:IsA("Model") then
parts[i] = getParts(p) --recursion
parts[i].Velocity = p.PrimaryPart.Velocity
end
end
end
local parts = getParts(game.Workspace)