I’m currently in the process of revamping my time stop system and I want to know how I can store the velocity of a part in a dictionary.
local parts = {} -- I only want to use 1 table.
for _, v in pairs(workspace:GetDescendants()) do
if v:IsA("BasePart") and not v.Anchored then
table.insert(parts, v) -- how can I also store the velocity?
v.Velocity = Vector3.new(0,0,0)
v.Anchored = true
end
end
wait(5)
for _, v in pairs(parts) do
-- how can I re-store the velocity to it's former state?
v.Anchored = false
end
If multiple parts have the same velocity, it might as well be wise to keep a table under a key, where the key is a velocity (a Vector3), and the table contains all the parts with that corresponding velocity.
This is what I’ve wound up doing, and it seems to work.
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}
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
we talked it over a bit in dms and changed some things:
the code above finds every part in the game and adding them to a table called parts
when a part’s velocity isn’t 0, a table containing the parts velocity to the parts table
(ex:parts[somePart] = {Velocity = somePart.Velocity} )
this means the parts table could end up looking something like this: parts = {part1, {Velocity = part1's velocity}, part2, part3, etc... }
when we loop through that in the second for loop, we are changing every value’s .Anchored property to false. some of the values in the table are tables which don’t have an anchored property.
bit of an oopsie because this would cause the script to error whenever a part has velocity since tables don’t have an anchored property.
we’re using the part as a key for our dictionary so to fix this, we can remove the table.insert line and get the part by using the key:
local parts = {}
for _, v in pairs(workspace:GetDescendants()) do
if v:IsA("BasePart") and not v.Anchored then
--table.insert(parts, v) REMOVED
if v.Velocity ~= Vector3.new(0,0,0) then
parts[v] = {Velocity = v.Velocity}
end
v.Anchored = true
v.Velocity = Vector3.new(0,0,0)
end
end
for part, partInfo in pairs(parts) do
--if parts[part] then we don't need this because the part will always be in parts since it's the key
part.Velocity = partInfo.Velocity
--end
part.Anchored = false
end