Moved or CframeChanged connection

I am currently trying to make something follow a part if it gets out of range, and unfortunately, I have to use a stepped connection. it would be much easier if we had this simple connection.

example:

Part.Moved:Connection(function(OldPos, NewPos)
       -- fires when the part changes position
end)
1 Like

Why can’t you just do this?

local oldPos = Part.CFrame
Part.Changed:Connect(function(value)
if value ~= oldPos then
-- run code here
end
end)

Why can’t you just use :GetPropertyChangedSignal()?

Part:GetPropertyChangedSignal("CFrame"):Connect(function()
       --code here
end)

unless you’re wanting the event to fire when it’s moved by the physics engine, but at that point you should just use PostSimulation or something.

2 Likes

It doesn’t update, at least for me

For performance reasons, CFrame updates do not happen if the object is not anchored. You need to make a RunService.Heatbeat connection instead.

3 Likes

This is a partially incorrect, changed signals for CFrame (and other PV properties) are not triggered by engine updates, but they are for changes made by scripts — regardless of if the assembly is anchored or not.

2 Likes

You should use Heartbeat for this kind of stuff, rather than Stepped.
Also, how would it be “easier” to use a connection rather than Stepped or Heartbeat? I assume you meant to say “more performant” rather than “easier”, but that is only partially correct. If you are concerned about using too many resources (which is very hard to achieve unless you have hundreds or thousands of lines of code), just reduce the amount of code being executed in the Heartbeat or Stepped.