Wheel Spinner "Click" Sound Not Working

I am trying to make a wheel spinner that plays a click sound every time the spinner passes a bar, but I believe the issue is that the wheel is spinning too fast for the script to recognize when the wheel has passed a bar. I also tried using math.round(360/7) instead of (360/7) and it still does not fully detect each rotation. Here is the code I am using:

local Tween = game:GetService("TweenService"):Create(Wheel, tweenInfo, {Rotation = v.Rotation + (360*Rotations)})
Tween:Play()
	
while Tween.PlaybackState == Enum.PlaybackState.Playing do
	
	if math.round(Wheel.Rotation%(360/7)) == 0  then -- There are 7 sections of the wheel. Every time the wheel is at one of these degrees it shoud play a sound, but only plays a sound occasionally when the wheel slows down towards the end of the tween.
		script.Parent.Click:Play()
	end
	game:GetService("RunService").RenderStepped:Wait()
end
Tween.Completed:Wait()

Is there another way I should detect when the wheel has passed a bar or is there another way to check more times per second? I already tried making the checker more lenient but that plays multiple clicking sounds in succession when the wheel slows down. All responses greatly appreciated, thanks.

Try putting a print there to see if the detection even works.

I already did that but you can tell the detection works because it plays sound.

I’m thinking the problem is that you are trying to use the :Play() function on the same sound instance way too fast. If this is the problem then you can fix it by making a sound instance, playing it and then destroying it when it’s done playing.

2 Likes

I’ve confirmed it’s that the script is not recognizing when the wheel is at a certain rotation, I added some code to the script that would add to a tally every time the rotation is correct for the sound to play and it returned.

Are you saying that the bit a script you added is telling you that it should have played the sound twice, but didn’t?

If so, then it seems @cakeycocoa has the right solution.

The script is telling me that it should have played the sound twice and it did, but I want the sound to place every time the wheel is at a specific rotation and the script is not recognizing when the wheel is at the correct rotation. The wheel spins very quickly so it skips the rotation I am looking for, which is why the script cannot recognize it.

Maybe there is an answer in this line of code:

math.round(Wheel.Rotation%(360/7)) == 0

If there are 7 sections, then Play the sound when it is a division of 7.

>= 51 < 103
>= 103 < 154
>= 154 < 206
>= 206 < 257
>= 257 < 308
>= 308 < 360
>= 360 < 51

or something like that.

That’s what my current code already does. I tried a variation of your solution and it still doesn’t register when the wheel is spinning at faster speeds and when the wheel spins slowly it plays the same sound repeatedly when the wheel is at the same rotation. The issue is the wheel spins too fast for the script to detect when it is at the correct rotation for the sound to play.

local Rotation = math.round(Wheel.Rotation)
local PossibleRotations = {51, 103, 154, 205, 257, 309, 0}
	
while Rotation >= 360 do
	Rotation = Rotation - 360
end
	
for i,v in PossibleRotations do
	if Rotation == v then
		script.Parent.Click:Play()
	end
end
game:GetService("RunService").RenderStepped:Wait()

Created a solution. Since there’s no way to check any faster than the tickrate, instead of looking for an exact number I just checked if the wheel’s rotation was at that number or had passed it.

while Tween.PlaybackState == Enum.PlaybackState.Playing do
						
	if Wheel.Rotation >= LastRotation + (360/7) then
		LastRotation = Wheel.Rotation
		script.Parent.Click:Play()
	end
	game:GetService("RunService").RenderStepped:Wait()
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.