Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

CFrame wheel of fortune?

  • Clickdetector can be used To make a spinning wheel (Like in Wheel Of Fortune), CFrame will be used instead of motors (Because in online mode, parts end up shaking)

Can someone help me with a CFrame script? Here is what I thought:

function onclicked()

(Wheel CFRAME script goes here)

end

(mouseclick script, I know this already)

1 answer

✓ Accepted answer

Ideally we would pick the option ahead of time and just use the spinning as a fancy transition. That lets us make sure all spots are fairly likely.

Let's say we have N equally sized slices on a single circular part (you could just use a decal for the divisions, or you'd modify this to appropriately weld or otherwise attach extra parts to this one).

local choice = math.random(0,N-1);
local angle = (choice + 0.5) / N * math.pi * 2; -- Angle of choice in radians
-- You may or may not want to add the 0.5.
-- Depending on how you split the regions this would
-- make it on the lines or in the middle of the regions

-- So that it doesn't obviously look fake, we should probably add
-- a random amount to it within the region.
angle = angle + (math.random() - 0.5) * math.pi * 2 / N;

To do the math, we have to pick a deceleration and a number of times to go around. The deceleration should be constant, but we can pick random numbers of times for it to revolve around:

local acceleration = -1; -- Units: radians / second
local rotations = math.random(10,15); -- Units: rotations (360deg)

angle = angle + rotations * math.pi * 2; -- Units: radians

Now we just have to solve for what initial speed will take us exactly there.

(This is math not Lua)
x: angle to reach
a: acceleration

t: total time (unknown)
v: initial velocity (unknown)

x = ((v + a * t) + v) / 2 * t;
v + t * a = 0

Solution
x = -a * t^2 / 2
v = -a * t
-2 * x / a = t^2
t = sqrt(-2 * x / a)
v = -a * sqrt(-2 * x / a)

So our initial velocity is

local initialVelocity = -acceleration * math.sqrt( -2 * angle / acceleration );

Now, we just animate:

local totalTime = initialVelocity / -acceleration;
local startTime = tick();
while startTime + totalTime > tick() do
	local time = tick() - startTime;
	local theta = (initialVelocity + initialVelocity + acceleration * time) / 2 * time;
	part.CFrame = CFrame.Angles(0, theta, 0) + position;
	wait();
end
-- In case it didn't quite make it in the time (0.03 seconds off) snap it to correct position:
part.CFrame = CFrame.Angles(0,angle,0) + position;

Where part is the part we are spinning (around the Y axis) and position is where you want the part to spin.


Note that when the animation stops, we already computed way ahead of time which slot it will land on, choice (a number 0 to N-1).

EDIT:

  • Fixed name typos with initialVelocity
  • Fixed while condition to use >; was <
  • Added randomness to initial angle choice

1 comment

0

Thank you. But I may need a bit of clarification, I am not that expirenced in scripting.

This discussion is preserved as it was. New answers, comments, and votes are closed.