Spinning a Wheel using ClickDetector

Hey there Robloxians!

Last time I made a Topic called “What script can make a wheel spin?” in here, the Roblox DevForum.

With that, I got an answer a user called Sleazel and I got this:

“I have made a sample script, as full rotations with TweenService are a bit tricky. ( Tween will always use shortest way to reach its goal, so in best case scenario you will only achieve half turn). To make full rotations you will need multiple tweens. I am also assuming you want your “Wheel of fortune” to slow down? Here is what I came up with (parent the script to the part, and make sure it is Anchored):”

local part = script.Parent
local TweenService = game:GetService(“TweenService”)
local slowestSpeed = 1
local spinStep = math.pi/36 --three steps per section
local friction = 0.5 --percent per step

local function Spin(initialSpeed)

if initialSpeed <= slowestSpeed then
	return
end
local speed = initialSpeed
local style = TweenInfo.new(1/speed,Enum.EasingStyle.Linear)
local target = {CFrame = part.CFrame * CFrame.Angles(0,spinStep,0)}
local tween = TweenService:Create(part,style,target)

local connection

local function OnCompletion()
	
	connection:Disconnect()
	speed = speed - (initialSpeed*friction/100)
	if speed < slowestSpeed then
		return
	end
	style = TweenInfo.new(1/speed,Enum.EasingStyle.Linear)
	target = {CFrame = part.CFrame * CFrame.Angles(0,spinStep,0)}
	tween = TweenService:Create(part,style,target)
	
	connection = tween.Completed:Connect(OnCompletion)
	tween:Play()
end

connection = tween.Completed:Connect(OnCompletion)
tween:Play()

end

Spin(50+math.random(50))

(Sorry If have many “>”, it’s because when I copied the text script, the text came but not the same way)

But the thing is, the script was working but the Wheel was rotating to wrong direction and this was the result when I put the script:

He also said this when I posted the photo:

Change the rotation axis in both targets. It will be either CFrame.Angles(spinStep,0,0) or CFrame.Angles(0,0,spinStep) depending on the wheel initial orientation.

And with that, I was pretty confused because I don’t have any experiences of programming or understading a thing called “CFrame”, I just have experiences of building.

I have no idea some type of stuffs people say like “You should use CFrame ” (I don’t know what CFrame means) because I don’t have that ability of programming, only building.

And I just told him the orientation to him

"The orientation from the wheel is 0, -90, 90"

And he never responded after that…

But being thinking more about the script that he gave in the Topic answer, the script doesn’t loop or can’t click to spin. And I finally realized that the part needed a thing called “ClickDetector”.

But I don’t know how a plaver can spin a wheel using the ClickDetector + what script for the Wheel to work.

Can someone help me out with this, please?

Thank you!

  • Juan
3 Likes

let’s imagine that theres a part with a ClickDetector inside of it in the workspace.

--Dont make new script just add this to a new line.
local ClickDetector = script.Parent

ClickDetector.MouseClick:Connect(function(playerWhoClicked)
    local speed = 1
    Spin(speed)
    -- That playerWhoClicked is their by default. You can change the name anything you want. it has the same value which is the Player who clicked the ClickDetector.
end)
2 Likes

So I just add this part with the other script that I mentioned in this topic?

1 Like

Add it on the script where the Spin(InitialSpeed) can be found. not inside of it but below of the function so the MouseClick function can call the Spin()

1 Like

I don’t see Spin(InitialSpeed) in my topic, just this in your reply that it’s similarSpin(speed)

(Sorry If I’m saying this that can be silly, It’s because I don’t understand nothing of scripts)

local part = script.Parent
local TweenService = game:GetService(“TweenService”)
local slowestSpeed = 1
local spinStep = math.pi/36 --three steps per section
local friction = 0.5 --percent per step

local function Spin(initialSpeed)

if initialSpeed <= slowestSpeed then
	return
end
local speed = initialSpeed
local style = TweenInfo.new(1/speed,Enum.EasingStyle.Linear)
local target = {CFrame = part.CFrame * CFrame.Angles(0,spinStep,0)}
local tween = TweenService:Create(part,style,target)

local connection

local function OnCompletion()
	
	connection:Disconnect()
	speed = speed - (initialSpeed*friction/100)
	if speed < slowestSpeed then
		return
	end
	style = TweenInfo.new(1/speed,Enum.EasingStyle.Linear)
	target = {CFrame = part.CFrame * CFrame.Angles(0,spinStep,0)}
	tween = TweenService:Create(part,style,target)
	
	connection = tween.Completed:Connect(OnCompletion)
	tween:Play()
end

connection = tween.Completed:Connect(OnCompletion)
tween:Play()
end

Spin(50+math.random(50))

I thought you’re using a multiple scripts because of different structure in your post. but I fixed the code format for you.

This is my answer to your post:

local part = script.Parent
local TweenService = game:GetService(“TweenService”)
local slowestSpeed = 1
local spinStep = math.pi/36 --three steps per section
local friction = 0.5 --percent per step

local function Spin(initialSpeed)

if initialSpeed <= slowestSpeed then
	return
end
local speed = initialSpeed
local style = TweenInfo.new(1/speed,Enum.EasingStyle.Linear)
local target = {CFrame = part.CFrame * CFrame.Angles(0,spinStep,0)}
local tween = TweenService:Create(part,style,target)

local connection

local function OnCompletion()
	
	connection:Disconnect()
	speed = speed - (initialSpeed*friction/100)
	if speed < slowestSpeed then
		return
	end
	style = TweenInfo.new(1/speed,Enum.EasingStyle.Linear)
	target = {CFrame = part.CFrame * CFrame.Angles(0,spinStep,0)}
	tween = TweenService:Create(part,style,target)
	
	connection = tween.Completed:Connect(OnCompletion)
	tween:Play()
end

connection = tween.Completed:Connect(OnCompletion)
tween:Play()
end

Spin(50+math.random(50))

-- Here is the ClickDetector!!!

local ClickDetector = part:FindFirstChild("ClickDetector") -- lets say the ClickDetector is a children of the part. the part is the (script.Parent) as you are assigning from above.

ClickDetector.MouseClick:Connect(function(playerWhoClicked)
    if ClickDetector then -- Lets check if theres a ClickDetector before running so error will not happen.
        print("ClickDetector found!")
        Spin(3) -- Calling the local function Spin(initialSpeed)
    else
        print("ClickDetector not found.")
    end
end)
1 Like

Now, I think I just to just add in the Wheel the ClickDetector and this script am I right?

If yes, then thank you for the amazing help Sorbious!!

1 Like

UPDATE: Appearently @Sorbious, I put the ClickDetector + the script but somehow the wheel doesn’t spin even putting UnAnchor or Anchor.

Look at this video:

robloxapp-20210105-0057337.wmv (888.2 KB)

(Sorry If it’s lagging, it’s the Roblox Recorder that makes the video like that when you press F12)

In the video, I tried clicking it but nothing seems to moved in the Wheel, very strange!