Detect when a part completely stops moving/falling?

So I’m trying to make performant falling parts. When they stop moving completely (falling from ceiling then hitting the floor), they’ll be anchored to reduce physics lag.

I don’t really have a script, which I know is needed but the farthest thing I could come up with is this:

script.Parent:GetPropertyChangedSignal("CFrame"):Connect(function(Position)
	print(Position)
end)

It doesn’t even work but what I’m trying to do is find a property that detects when something is still, CFrame, AssemblyAngular/LinearVelocity, etc and nothing seems to work.

1 Like

I believe when a part is falling/moving, The Velocity property will change as well.

local past = Vector3.new()
local current = part.Position
local isStill = false

game:GetService("RunService"):Connect(function()
current = part.Position
if (current - past).Magnitude < 0.01 then
  isStill = true
else
isStill = false
end
past= part.Position
end)

3 Likes

Thank you so much!

For anyone else reading who wants to create performant falling parts (anchoring still parts for performance boost), I suggest adding a count system, so if the part bounces and the magnitude decreases insanely for even a short second, the part won’t get anchored mid air.

Incase I’m terrible at explaining, I’ll just post the modified script here:

local Part = script.Parent
local Past = Vector3.new()
local Current = Part.Position
local Count = 0

while true do
	Current = Part.Position
	if (Current - Past).Magnitude < 0.01 then
		Count += 1
		if Count > 3 then
			Part.Anchored = true
			Part.CanCollide = false
			break
		end
	end
	Past = Part.Position
	wait(0.1)
end

Ty @CoderHusk and @Kriosynic

2 Likes

You probably don’t need to check every single frame as it might cause even more lag than it reduces. Checking every couple of seconds is enough.

1 Like