If you’ve never watched Wheel of Fortune or some other game with a massive wheel, the
point of the wheel is it to spin and land on a random tile.
I’m not looking for a script to make the wheel (I already have a decal for that): I’m looking for a script that would spin the wheel on click.
My only issue is going about randomly generating how long the wheel will spin: my guess is that the wheel would randomly generate a random number in CFrame and then rotate that many times. How would I go about doing so?
EDIT: Another problem: would it be possible to weld a wheel to spin as well using a welding script? If I wanted to add those little pegs on the sides that the pointer would fall on, would it be possible to weld the wheel to these pegs?
For your first question about making a randomly spinning wheel. You can use mouse.Target to get what your mouse is clicking on or you could use a clickDetector. You can use math. random(1, someNumber) to get how long you want it to spin for. Then you could CFrame the wheel a little bit at time until your random number is reached.
I already know how to click the wheel and make it activate: I just don’t know how to make it spin. Do I randomly generate how long it spins, or the distance it spins?
You can use CFrame.Angles on the axis you want it to spin by a random starting speed, then you will decrease the angular speed over time at a rate you specify (deceleration):
Here’s some pseudo-code:
local r = 2
local v = math.random(20, 50)
local con
con = game:GetService("RunService").RenderStepped:Connect(function(dt)
wheel:SetPrimaryPartCFrame(wheel:GetPrimaryPartCFrame() * CFrame.Angles(0, dt * v, 0)) -- if your wheel is a model
v = math.max(v - (dt * r), 0)
if v == 0 then
con:Disconnect()
end
end)
This is if you’re running it in a local script
EDIT: I clamped the angular speed at a minimum of 0 and disconnected the event when it reaches 0
1). I would try the method that @Raretendoblox shared, render stepped event and some minor CFrame math on the client is a good way to go.
2). you wouldn’t have to weld the wheel to anything, you can just anchor it. (anchoring it wont stop the CFrame from rotating)
3). [Optional] factors for spinning the wheel should probably be sent from the server, (like the random starting speed and the amount of decrease for the angular speed).
Thanks! This is incredibly useful, but I know from prior knowledge that RenderStepped can only be used in Local Scripts. Is it possible to translate it into a normal script so everyone in the server can see?
Stepped is an alternative, which fires before every physics update, but using RenderStepped on the client should be a little smoother. You can notify all clients when you click the wheel and spin on their clients to make it a little smoother.
If you don’t want to do all of that, you can just use Stepped on the server
Sorry to be of nuisance, but the part spins very very fast. I’ve tried adjusting both the r and v values but to no avail. Any way to slow it down a bit?
Don’t use RenderStepped here. You aren’t updating something that needs to be changed before a frame renders. Furthermore, the way you’re using it can delay frame execution.
This item is also not physics-reliant and needing to update it before physics are simulated is not necessary. If you needed it updated before physics were applied, that would be a different story.