Here is the code I am using to detect the last time at which a player’s character was moved. This program is run in a local script, which is a descendent of the character. Whenever I move the character, nothing prints and the variable is not updated. No errors are visible in the output, and I can confirm that these lines of code are running. Does anyone know what I’m doing wrong?
Instance:GetPropertyChangedSignal() or Instance.Changed doesn’t fire for physics related things such as CFrame or Position.
The developer hub recommends just using a while loop
From the wiki:
This event does not fire for physics-related changes, like when the
CFrame,Velocity,RotVelocity,Position,OrientationandCFrameproperties of aBasePartchange due to gravity. To detect changes in these properties, consider using a physics-based event likeRunService.SteppedorBasePart.Touched. A while-true-do loop can also work.
What I would do:
local lastMoved = os.time()
local lastP = script.Parent.Parent.HumanoidRootPart.Position
game:GetService("RunService").Stepped:Connect(function(dt)
-- Check if the part moved since the last physics step.
-- (within a certain small deviation, hence the use of FuzzyEq)
if (not script.Parent.Parent.HumanoidRootPart.Position:FuzzyEq(lastP)) then
print("moved")
lastMoved = os.time()
-- Set up for comparison in the next step.
lastP = script.Parent.Parent.HumanoidRootPart.Position
end
end)
2 Likes
Lovely, how efficient! Thank you.
