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.)
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
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)