Why this isnt working?

Hello, Im doing a volume changer system but Im stuck and I dont know how to continue it, here is one image of it
image

I actually have no idea how to do it so I decided to ask chatGPT how to do it, it gave me a code but it works really bad, here is it:

local UserInputService = game:GetService("UserInputService")

local music = game.ReplicatedStorage:WaitForChild("BackroundMusic",wait())
local InitialMusicVolume = music.Volume
local FULL_VOLUME_VARIABLE = InitialMusicVolume / 100

local BarDetector = script.Parent.Parent["Music Line"]
local VolumeSlider = script.Parent

local function SetVolume()
	local sliderPosition = math.clamp(UserInputService:GetMouseLocation().X - VolumeSlider.AbsolutePosition.X, 0, BarDetector.AbsoluteSize.X)
	local maxSliderPosition = BarDetector.AbsoluteSize.X - VolumeSlider.AbsoluteSize.X
	local percentage = sliderPosition / maxSliderPosition

	local newVolume = math.clamp(percentage, 0, 1) * FULL_VOLUME_VARIABLE
	music.Volume = newVolume
	VolumeSlider.Position = UDim2.new(sliderPosition, 0, 0, 0)
end

local function ConnectVolumeChange()
	UserInputService.InputChanged:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.MouseMovement and UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) then
			SetVolume()
		end
	end)
end

local function DisconnectVolumeChange()
	VolumeSlider.MouseMoved:Disconnect()
end

VolumeSlider.MouseButton1Down:Connect(ConnectVolumeChange)
VolumeSlider.MouseButton1Up:Connect(DisconnectVolumeChange)

I would be really happy if someone can help me.

What is not working?, when i drag the textbutton, it moves but goes up like the image below, and the volume is going to 0.
image

1 Like

Yepp, that’s where the problem starts…
ChatGPT is good at answering questions if it can find the answer online. ChatGPT, however, is terrible at creating its own “stuff” or thinking. That’s why its codes fail (mostly). You should have just looked on the DevForum for a tutorial or a similar post, there are a lot of them.

Please look around online if you can find a post that’s already answered before you post one too, and don’t use ChatGPT for Roblox programming.

2 Likes

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