How do I detect if a part is moving/rotating?

So I’m making a dice game and I am trying to wait untill the dice have stopped moving to detect which side of the dice is on top. Here is my code:

repeat HasStopped = DetectRotation() until HasStopped == true 

How would I go about making that DetectRotation() function? I want it to detect if the die is moving/rotating and return true if it is, but return false if it is not. Any suggestions?

(There is no wait in the repeat loop because I figured I would have to wait a little bit in the DetectRotation() function to detect the property changes.)

Interesting problem, maybe inside the DetectRotation function, you can have something like this:

FirstCFrame = part.CFrame
wait(0.5)
SecondCFrame = part.CFrame
return compare(FirstCFrame, SecondCFRame)

You should define the compare function so that it returns true if both CFrames are almost the same.

It dose not seem to recognize the function compare()

Yeah i wanted you to define that function yourself. For now, try doing

return FirstCFrame == SecondCFrame

and see if that works

You can use a simple script to check the magnitude of the part which can be used as the speed.
This script gets the magnitude between the part’s positions, which if it’s greater than 0 that means that the part is moving.

-- variables
local moving_part  "PART HERE"
local last_position = moving_part.Position
local check_dealy = 0.1

-- loop checking movment
while wait(check_delay) do
    local curr_position = moving_part.Position
    local difference = (last_position - curr_position) / delay_time
    last_position = curr_position
    if difference.Magnitude > 0 then
        -- part is moving
    end
end

Maybe something like:

local function IsStopped(part)
  return part.AssemblyAngularVelocity.Magnitude < 0.0001 and part.AssemblyLinearVelocity.Magnitude < 0.0001
end

And then

local function AfterStopDo(part, func)
	local conn
	conn = game:GetService("RunService").Heartbeat:Connect(function()
		if IsStopped(part) then
			conn:Disconnect()
			func()
		end
	end)
end


-- usage e.g.

AfterStopDo(workspace.Part, function()
	print("The part has stopped!")
end)

did you mean / check_delay?

Yes I did! My bad haha :sweat_smile: