I have created a simple die roll function that flips the die to the random result generated using stored rotations in CFrame values within the die. It works for the most part, but I want to get rid of glitch at the top of the path, you can see it suddenly jerks to some unknown rotation and then returns to where it is supposed to be. I don’t know what causing this as nothing is really happening between the two tweens playing.
Video:
Code:
local tweener = game:GetService("TweenService")
local rand = Random.new()
local THROW = 5
local TWEENINFOUP = TweenInfo.new(0.2,Enum.EasingStyle.Quint,Enum.EasingDirection.Out)
local TWEENINFODOWN = TweenInfo.new(0.2,Enum.EasingStyle.Quint,Enum.EasingDirection.In)
function roll(die)
local result = rand:NextInteger(1,tonumber(die.Name:match("%d+")))
local dieStart = die.CFrame
--Get rotation information stored in CFrameValue
local x,y,z,R00,R01,R02,R10,R11,R12,R20,R21,R22 = die:FindFirstChild(tostring(result)).Value:GetComponents()
local tweenUP = tweener:Create(die,TWEENINFOUP,{CFrame = CFrame.new(dieStart.X,dieStart.Y+THROW,dieStart.Z,-R00,R01,-R02,R10,-R11,R12,-R20,R21,-R22)})
local tweenDown = tweener:Create(die,TWEENINFODOWN,{CFrame = CFrame.new(dieStart.X,dieStart.Y,dieStart.Z,R00,R01,R02,R10,R11,R12,R20,R21,R22)})
tweenUP:Play()
tweenUP.Completed:Wait()
tweenDown:Play()
tweenDown.Completed:Wait()
tweenDown:Destroy()
tweenUP:Destroy()
print(result)
end
workspace.Event.Event:Connect(function() roll(workspace.D6) end)