Slider GUI with Audio Issue

Hey there, So I am working on a small project for myself - It isn’t going public, it’s staying just me. I am attempting to make a throttle system, where if it reaches 25% on the throttle, E1 starts, 50% e2, so forth to 100%. There is one I have seen, although I cannot replicate a similar method to this. As you can see here, This is what I am trying to make.

I have managed to make something similar but, When I move the slider, the audios play although it’s goes from 0 => 0.1 => 0.2 => 0 on TimePosition ( Even without moving). Is there anything I can sort of do to make the audio play correctly? (I know about the audio issue ongoing aswell.)

Here’s my script.

		local holder = script.Parent.Holder

		local slide =holder.Bar
		local slider = holder.Bar.Slide

		local N1 = script.Parent.N1
		local percentage = script.Parent.Percentage

		local mouse = game.Players.LocalPlayer:GetMouse()
		local air
		local snap = .5
		local pixelsfromedge = 1.25

		local moving = false

		local sounds = game.ReplicatedStorage.Sounds

		slider.MouseButton1Down:Connect(function()
			moving = true
		end)
		slider.MouseButton1Up:Connect(function()
			moving  = false
		end)

		mouse.Button1Up:Connect(function()
			moving = false
		end)
		local rpms = nil
		mouse.Move:Connect(function()
			if moving then
				local xOffset = math.floor((mouse.X - slide.AbsolutePosition.X) / snap + .5) * snap
				local xOffsetClamped = math.clamp(xOffset, pixelsfromedge, slide.AbsoluteSize.X - pixelsfromedge)

				local newposslid = UDim2.new(0, xOffsetClamped, slider.Position.Y)

				slider.Position = newposslid

				local roundabssize = math.floor(slide.AbsoluteSize.X / snap + .5) * snap
				local clamped = math.floor(xOffsetClamped / snap + .5) * snap

				local slidbal = clamped / roundabssize
				local rpm = slidbal * 100
				wait()
				local number = math.floor(rpm)
				percentage.Text = rpm
				--print(number)
				rpms = number
			end
		end)


		while true do
			wait()
			if type(tonumber(rpms)) == "number" then
				if tonumber(rpms) > 0 then
					local c = sounds.GE90_rpm1:Clone()
					c:Play()
					c.TimePosition = 0
					--sounds.GE90_rpm1_Turbine:Play()
					wait(1 or sounds.GE90_rpm1.TimeLength)
				else
					sounds.GE90_rpm1:Stop()
					--sounds.GE90_rpm1_Turbine:Stop()
				end
				if tonumber(rpms) >=  25 then
					local c = sounds.GE90_rpm2:Clone()
					c.TimePosition = 0
					c:Play()
					wait(1 or sounds.GE90_rpm2.TimeLength)
					--	sounds.GE90_rpm2_Turbine:Play()
				else
					--	sounds.GE90_rpm2_Turbine:Stop()
					sounds.GE90_rpm2:Stop()
				end
				if tonumber(rpms) >= 50 then
					local c = sounds.GE90_rpm3:Clone()
					c.TimePosition = 0
					c:Play()
					wait(1 or sounds.GE90_rpm3.TimeLength)
				else
					sounds.GE90_rpm3:Stop()
					--sounds.GE90_rpm3_Turbine:Stop()
				end
				if tonumber(rpms) >= 75 then
					local c = sounds.GE90_rpm4:Clone()
					c.TimePosition = 0
					c:Play()
					wait(1 or sounds.GE90_rpm4.TimeLength)
					--sounds.GE90_rpm4_Turbine:Play()
				else
					sounds.GE90_rpm4:Stop()
					--sounds.GE90_rpm4_Turbine:Stop()
				end
			end
		end

If you can help, that’d be amazing!

Hi! If you haven’t already, could you please put a print statement right before each time you play a sound in your script? That might make it clear if it’s an issue with when you’re playing your audio or how you’re playing it.

Based on this quote, my guess as to what’s really going on is that you’re playing your audio from the beginning more often than you should. Printing before you play/stop any audio can help localize your issue.

I have used the print feature, and the position is on 100% and has provided me with this.

I also have parented the cloned audios aswell.

It has cloned but more than once, multiple times now after all printing and fixing issues.

as shown here:
image

Instead of checking the rpms variable in an infinite loop, why not check them inside of the mouse.Move event? At the end of that event, you’re setting the rpms variable to a number. That’s where you need to check which sound to play and play it. Using an infinite loop is unnecessary and might slow down your game or script.

There’s also no need to clone the sound object before you play it. If you want the sound to come from a specific part, then parent all of your sound objects into that part inside the explorer and simply reference them from the script. Alternatively, if you don’t want them to come from any specific location, you can parent the sound objects to the SoundService. If you do need to create the sounds from the script, you can just do it once at the top instead of every time. This will stop your explorer from filling up like it did in the picture.

In terms of actually solving your bug, you’ll want to first enable looped in your sound objects. Then, you’ll need to make the following changes:

What you need to do

At the top of your script, create a variable named something along the lines of prevAudio. Set it’s value to nil.

At the very end of your mouse.Move event
a) Check that the number variable is actually a number
b) Using if/elseif statements, check to see if the number variable is within a certain range.
c) If it is, check to see if prevAudio is not equal to nil. If it isn’t equal to nil, stop prevAudio. If it is equal to nil, just continue.
d) Play the correct audio
e) Set prevAudio to be equal to the audio that is currently playing
f) Return so that the if statement doesn’t continue.

You can delete the rpms variable, it’s not really necessary. Here’s the code:

-- At the top of the script, along with your other variables
local prevAudio = nil

-- Inside the mouse.Move event, at the end of the function
if tonumber(number) then
	number = tonumber(number)

	if number < 25 then
		local sound = -- The sound you want to play
		if prevAudio ~= nil then
			prevAudio:Stop()
		end

		sound:Play()
		prevAudio = sound
		return
	elseif number < 50 then
		local sound = -- The sound you want to play
		if prevAudio ~= nil then
			prevAudio:Stop()
		end

		sound:Play()
		prevAudio = sound
		return
	elseif -- Continue for all 4 audios
end

Do keep in mind that I’m not 100% sure that this works. Hope this helps!

Hey, Thanks for the help! It works like a charm now!

1 Like