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

How would I make it so if I clicked a radio it would turn of the music?

local mesh = script.Parent.Mesh
local music = script.Parent.Music
local playingmusic = false

function choosemusic()
	music.Volume = 0.1
	music.TimePosition = 0
	local song = math.random(1, 12)
	if song == 1 then
		music.SoundId = "rbxassetid://406637473"
	end
	if song == 2 then
		music.SoundId = "rbxassetid://406637473"
		music.TimePosition = 1
	end
	if song == 3 then
		music.SoundId = "rbxassetid://406637473"
	end
	if song == 4 then
		music.SoundId = "rbxassetid://406637473"
	end
	if song == 5 then
		music.SoundId = "rbxassetid://672731096"
	end
	if song == 6 then
		music.SoundId = "rbxassetid://386781032"
	end
	if song == 7 then
		music.SoundId = "rbxassetid://386781032"
		music.TimePosition = 1.5
	end
	if song == 8 then
		music.SoundId = "rbxassetid://386781032"
	end
	if song == 9 then
		music.SoundId = "rbxassetid://386781032"
	end
	if song == 10 then
		music.SoundId = "rbxassetid://791467527"
		music.Volume = 0.3
	end
	if song == 11 then
		music.SoundId = "rbxassetid://791467527"
	end
	if song == 12 then
		music.SoundId = "rbxassetid://791467527"
	end
	music:Play()
end

while true do
	music:Stop()
	wait(5)
	choosemusic()
	playingmusic = true
	coroutine.resume(coroutine.create(function()
		repeat
			wait()
			mesh.Scale = mesh.Scale + Vector3.new(0.025, 0.025, 0.025)
			wait()
			mesh.Scale = mesh.Scale + Vector3.new(0.025, 0.025, 0.025)
			wait()
			mesh.Scale = mesh.Scale + Vector3.new(0.025, 0.025, 0.025)
			wait()
			mesh.Scale = mesh.Scale - Vector3.new(0.025, 0.025, 0.025)
			wait()
			mesh.Scale = mesh.Scale - Vector3.new(0.025, 0.025, 0.025)
			wait()
			mesh.Scale = mesh.Scale - Vector3.new(0.025, 0.025, 0.025)
	until playingmusic == false
	end))
	wait(130)
	playingmusic = false

local isOn = true
local music = script.Parent.Music

function on()
	music:Play()
end

script.Parent.click.ClickDetector.MouseClick:connect(OnClicked())

function off()
	music:Stop()
end

function OnClicked()

    	if isOn == true then isOn = false off()
        isOn = false
        off()
    else
        if isOn == false then isOn = true on()
        on()
		end
    end
end


script.Parent.click.ClickDetector.MouseClick:connect(OnClicked)


on()

end


This is the whole code for the radio and at the bottom is where I want it to be able to turn off and on again (Sorry for wasting you're time i'm just new and have no clue what to do)

1 comment

0

If anyone could tell me what's wrong with the script and help me that would be nice

2 answers

Did you get the off() and on() functions mixed up? the off() function is playing the music whilst the on() function is stopping the music.

3 comments

0

Please read and interpret the code before giving an answer

0

It does seem like I mixed up the on and off function.. but when i switched it and then I clicked the radio it didn't turn off

0

You need to use the isOn variable and set it to true/false depending on if the radio is turned of or not. something like "if isOn == true then isOn = false off()" and "if isOn == false then isOn = true on()"

Hello, my name is NinjaManChase and I'll try to answer your question as simply as possible :)

It appears that you never toggled isOn. This means that the script always thinks your radio is offline.

We can fix your code by replacing your onClicked function with:

function OnClicked()

	if IsOn then
		isOn = false
		off()
	else
		isOn = true
		on()
	end
end

Hope this helped! Leave a comment if you need further assistance

2 comments

0

Hello, and thanks for assisting me, but it appears when I click it, it doesn't turn off.. if I wasn't clear. When you go into game the radio is playing.

0

turn isOn to true or run off before the OnClicked function in your script

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