I’ve gotten a little stuck in this issue so I’m reaching out to devforum to ask for help.
Basically what I want to achieve is to detect if a parts velocity has changed. And no, I can’t use GetPropertyChangedSignal. It seems like that function doesn’t detect physics related velocity updates but instead code-related variable updates.
The reason why I want to do this is for optimisation purposes, I could use a heartbeat function to continuously check if a part has changed its velocity over time but having multiple heartbeats run constantly and or simultaneously is not the most fantastic technique when It comes to minimise lag in a game on Roblox. That’s why having a function made to only run whilst a certain part is moving is a much more approachable solution since it won’t run in the background when it doesn’t need to, eliminating expensive or tedious computations for the computer to do every frame
.Changed doesn’t really work when I’m trying to detect movement, oddly enough. Could you give me more insight on what you mean with using AssemblyLinearVelocity and .changed?
Let me explain again, I want to detect physics-based velocity changes! That means not changing a variable of the part inside the code! If I was to remove this line It would never work when pushing the part with the player
local PartList = workspace.PartFolder:GetChildren()
local MovingPartList = {}
for _,Part : Part in PartList do
Part:SetAttribute("Moving",false)
Part:SetAttribute("Position",Part.Position)
end
game["Run Service"].Stepped:Connect(function()
for _,Part :Part in MovingPartList do
-- do somethink
end
end)
while task.wait(1.0) do
for _,Part : Part in PartList do
if Part:GetAttribute("Moving") == false and (Part:GetAttribute("Position") - Part.Position).Magnitude > 1 then
table.insert(MovingPartList,Part)
Part:SetAttribute("Moving",true)
elseif Part:GetAttribute("Moving") == true and (Part:GetAttribute("Position") - Part.Position).Magnitude < 1 then
table.remove(MovingPartList,table.find(MovingPartList,Part))
Part:SetAttribute("Moving",false)
end
Part:SetAttribute("Position",Part.Position)
end
end
I think the only way to check about part velocity - it’s checking all part position. And i think you was looking for somethink like this for optimisation
You don’t need to have multiple heartbeats do a certain task for multiple instances. What you could do instead is have one heartbeat event connected and loop through a table of those instances and do something with them.
For example
local affectedInstances = {}
local function addToAffectedInstances(inst)
if table.find(affectedInstances, inst) then return end
return table.insert(affectedInstances, inst)
end
local function removeFromAffectedInstances(inst)
local index = table.find(affectedInstances, inst)
if index == nil then return end
return table.remove(affectedInstances, index)
end
game:GetService("RunService").HeartBeat:Connect(function(dt)
for _, v in pairs(affectedInstances) do
--do something
end
end)
for i, v in pairs(workspace:WaitForChild("chairsFolder"):GetChildren()) do
addToAffectedInstances(v)
end
Here basically something happens to all the children of the “chairsFolder” every heartbeat, with just one heartbeat event connected.
If you want to check for the velocity of each chair (for example assuming that it is a model), then you have to have some kind of primaryPart of that model to check its velocity.
Ah, well I guess I’ll go with this solution! Originally I thought of doing this but I was not sure how I would get all doors. Placing it in a folder solves that very easily, Thanks!
You could also tag the door models individually and get all tags using CollectionService. This is also a very little bit faster (I tested it before), and you don’t need to put all doors inside a folder.
Additionally, you could check if a door gets a “door” tag, the script calls addToAffectedInstances() on it, and also checks if a door has its “door” tag removed, then removeFromAffectedInstances() gets called.