Hey! I am in the works of a new project and I could use feedback on my “Spin the wheel”.
The wheel model is just for testing purposes, I will beautify the appearance of it later on.
How it works:
- The wheel uses Angular Velocity to give it more of a “realistic” approach.
- After a random amount of time, the wheel will then come to a complete stop.
- Raycasting is used to determine what the wheel has stopped at.
The main functions are in a module script and ran server sided.
Module Code
local wheel = {}
function WheelAction(angVel) -- Creates and plays wheel spinning tweens
local tweenService = game:GetService("TweenService")
local spinInfo = TweenInfo.new(0.4, Enum.EasingStyle.Back, Enum.EasingDirection.In);
local stopInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out);
local spinTween = tweenService:Create(angVel, spinInfo, {AngularVelocity = Vector3.new(500,0,0)});
local stopTween = tweenService:Create(angVel, stopInfo, {AngularVelocity = Vector3.new(0,0,0)});
spinTween:Play()
spinTween.Completed:Wait()
delay(math.random(1,3), function()
stopTween:Play()
stopTween.Completed:Wait()
end)
end;
function wheel.Spin(object) -- Initiates wheel spin system
local wheel = object:FindFirstChild("Wheel")
local velocity = wheel:FindFirstChild("AngularVelocity", true)
local clip = object:FindFirstChild("Clip")
if wheel and clip and velocity then
WheelAction(velocity)
end
end;
function wheel.Select(object) -- Fires a raycast
local caster = object:FindFirstChild("Clip")
local rayOrigin = caster.Position
local rayDirection = Vector3.new(0,-10, 0)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {object:FindFirstChild("Choices")}
raycastParams.FilterType = Enum.RaycastFilterType.Whitelist
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
if raycastResult then
local hitPart = raycastResult.Instance
if hitPart.Parent == object:FindFirstChild("Choices") then
return hitPart.Name
end
end
end;
return wheel
Server Code
local wheel = workspace:WaitForChild("Spinner")
local mod = require(script.Parent.Parent.Modules.Wheel)
while wait(6) do
mod.Spin(wheel) --
repeat
wait(1)
-- Waits for the wheel to completely stop
until wheel:WaitForChild("Wheel").AssemblyAngularVelocity == Vector3.new(0,0,0)
local value = mod.Select(wheel)
if value ~= nil then
print("The wheel has landed on "..value..".")
end
wait(1)
end
I’d love to hear any critique I can gain from this whether it would be different ways to create the wheel model, more efficient methods of coding the wheel, better coding practices, etc.
If anything is unclear please don’t hesitate to ask for clarification.
Thank you for reading and more importantly thank you for your time!
edit: I forgot to mention that the server code is only for testing purposes.