Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Button doing function reverse?

I'm making a mute button to mute elevator music for my elevator game, I'm making a mute elevator button so players can mute the music in the elevator, But I wrote a code and it doesn't work, I've been trying to fix it for 2 hours now, May anyone help please?

The problem is that it doesn't play at first, When it's supposed to be off it is and the button is on, When it's off it says off and plays music, I press it again, Still plays music and says on.

My codes

DisableMusic Local Script

local on = true
local player = game:GetService("Players").LocalPlayer

local musicParent = workspace:FindFirstChild("Elevator"):FindFirstChild("TimerPreview"):FindFirstChild("MusicPlayer")

script.Parent.MouseButton1Click:Connect(function()
	if on == true then
		on = false
		game:GetService("ReplicatedStorage"):FindFirstChild("Mute"):FireServer()
		script.Parent.BackgroundColor3 = Color3.fromRGB(85, 255, 0)
		script.Parent.Text = "On"
	else
		on = true
		game:GetService("ReplicatedStorage"):FindFirstChild("Unmute"):FireServer()
		script.Parent.BackgroundColor3 = Color3.fromRGB(170, 0, 0)
		script.Parent.Text = "Off"
	end
end)

Section of MainHandler aka the Elevator script. Note: I am running the function

function chooseRandomSong()
	
	if chosenSong == false then
		
		local allMusic = musicFolder:GetChildren()

		local randomMusic = allMusic[math.random(1,#allMusic)]
		local cloneMusic = randomMusic:Clone()
		
		for i, player in pairs(game:GetService("Players"):GetPlayers()) do
			if player then
				if player:WaitForChild("MutedMusic").Value == false then
					game:GetService("ReplicatedStorage"):FindFirstChild("UnmuteClient"):FireClient(player)
				else
					game:GetService("ReplicatedStorage"):FindFirstChild("MuteClient"):FireClient(player)
				end
			end
		end

		cloneMusic.Parent = musicPlayer
		
		game:GetService("ReplicatedStorage"):FindFirstChild("Mute").OnServerEvent:Connect(function(player)
			
			if player:WaitForChild("MutedMusic").Value == true then
				
				game:GetService("ReplicatedStorage"):FindFirstChild("MuteClient"):FireClient(player)
				
			end
			
		end)
		
		game:GetService("ReplicatedStorage"):FindFirstChild("Unmute").OnServerEvent:Connect(function(player)

			if player:WaitForChild("MutedMusic").Value == false then
				
				game:GetService("ReplicatedStorage"):FindFirstChild("UnmuteClient"):FireClient(player)
				
			end

		end)
		
		local songNameDefault = cloneMusic.SoundId:match("%d+")
		
		local songId = cloneMusic.SoundId
		local newStr, replaced = string.gsub(songId, "rbxassetid://", "")
		
		local songName = MarketPlaceService:GetProductInfo(songNameDefault,Enum.InfoType.Asset).Name
		
		print(songName.." has been selected as song")
		
		game:GetService("ReplicatedStorage"):FindFirstChild("UpdateNameGui"):FireAllClients(songName, newStr)
		
		chosenSong = true
		
	end

MusicMain Local Script

game:GetService("ReplicatedStorage"):FindFirstChild("MuteClient").OnClientEvent:Connect(function()

	for i, v in pairs(workspace:FindFirstChild("Elevator"):FindFirstChild("TimerPreview"):FindFirstChild("MusicPlayer"):GetChildren()) do
		if v:IsA("Sound") then
			v:Stop()
		end
	end

end)

game:GetService("ReplicatedStorage"):FindFirstChild("UnmuteClient").OnClientEvent:Connect(function()
	
	for i, v in pairs(workspace:FindFirstChild("Elevator"):FindFirstChild("TimerPreview"):FindFirstChild("MusicPlayer"):GetChildren()) do
		if v:IsA("Sound") then
			v:Stop()
		end
	end		

end)

1 comment

0

Nvm guys, I fixed it

1 answer

You can try playing the sound after MainHandler chose a song

1 comment

0

Yeah but I'm trying to play for every client that hasn't muted it

This discussion is preserved as it was. New answers, comments, and votes are closed.