Audio volume always becomes 10 when getting value from TextBox

  1. What do you want to achieve: I want to locally change the volume of sound(s) using a textbox and local script

  2. What is the issue: I’m getting this issue where, after entering any value, the Sound’s volume is always becoming 10

  3. What solutions have you tried so far: I’ve looked at this post and after some improvising worked completely fine yesterday until I return to work on my project and it’s all broken

This code took text from a textbox(button) and made it the volume of the sound, its input/100 because I wanted the volume in the text to be out of 100 (I.E type 50 and the sound volume is 0.5). Jukebox is a folder containing the sound (creb) It worked yesterday and it’s broken now

local button = script.Parent
local songs = {
	game.SoundService.Jukebox.Creb; 
}
local input = tonumber(button.Text)


function adjustSound(volume)
	for _, object in next, songs do
		if input<100 then
			object.Volume = input/100
		else
			object.Volume = 0.5
			button.Text = "50"
		end
	end
end

button.FocusLost:Connect(adjustSound)

I was getting an error (attempted to compare nil < 100) and I downsized the code to

local button = script.Parent
local songs = {
	game.SoundService.Jukebox.Creb; 
}
local input = tonumber(button.Text)


function adjustSound(volume)
	for _, object in next, songs do
		object.Volume = input
	end
end

button.FocusLost:Connect(adjustSound)

The volume always becomes 10 and I just want the number in the text to be the same as the number in the Volume properties (locally)

P.S: This is my first DevForum post, literally, so if I make a mistake I’d highly appreciate one of you pointing it out and I’ll gladly fix it. Keep in mind that I’m trying my best.

Well, no particular ideas so far why 10 exactly, but I see two small things to correct in your code.

  1. You only need one FocusLost connection. And in adjustSound(volume), the “volume” would actually be a boolean signifying whether enter was pressed to lose focus.

  2. You’re storing input in a variable at the top. The input value is never being changed. So perhaps it was 10 in the TextBox before some script changed it to 50 as “default”. Always take a new button.Text each time focus is lost.

Also, don’t forget to convert the input string into the number: tonumber(button.Text).

And, welcome to the Dev Forum!

1 Like

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