Spinning Wheel Feedback

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.

Wheel Visual

How it works:

  1. The wheel uses Angular Velocity to give it more of a “realistic” approach.
  2. After a random amount of time, the wheel will then come to a complete stop.
  3. 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.

3 Likes

The new TorsionSpringConstraint might come in handy for your wheel. Its announcement actually features a spinning wheel to demonstrate the constraint (see Introducing TorsionSpringConstraint).

4 Likes

Do you have a game where we can test it?

Of course! I haven’t got around to updating the wheel much but here is a link if you
would still like to test it.

Thanks! I really like it. This is to make it over 3O characters.

1 Like

The Torsion spring constraint it doesn’t work i hate this constraint

It’s pretty well made, though I’d suggest pre-loading the selected item on the wheel on the server in order to ensure you’re hitting the right value. Once you do this, just send it to client and perform the tween and make it land on the selected item