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

Help to modify a script?

Basically I want this script to stop when I press the button for a second time. So I press it once and it starts but I want it so that when I press it for a second time that it would not play the music again it would turn it all off.

function onClick()
    while true do
        script.Parent.Parent.Sound1:Play()
        wait(120)
        script.Parent.Parent.Sound1:Stop()
        wait(.1)
        script.Parent.Parent.Sound2:Play()
        wait(120)
        script.Parent.Parent.Sound2:Stop()
        wait(.1)
        script.Parent.Parent.Sound3:Play()
        wait(120)
        script.Parent.Parent.Sound3:Stop()
        wait(.1)
        script.Parent.Parent.Sound4:Play()
        wait(120)
        script.Parent.Parent.Sound4:Stop()
        wait(.1)
        script.Parent.Parent.Sound5:Play()
        wait(120)
        script.Parent.Parent.Sound5:Stop()
        wait(.1)
        script.Parent.Parent.Sound6:Play()
        wait(120)
        script.Parent.Parent.Sound6:Stop()
        wait(.1)
    end
end


script.Parent.ClickDetector.MouseClick:connect(onClick)

Thanks Kieranm9090

4 answers

You used a loop, you know what is it? A loop will call everything inside once every delay, that's why your code will loop the function infinite times and doesn't respond to your commands, you need something like this:

I suppose that you created a variable called "soundStarted" and "soundNumber"

local sounds = { "Sound1", "Sound2", "Sound3", "Sound4", "Sound5" } -- Your sounds, all in the same path!!
local soundStarted = false
local soundNumber = 0 -- We will start from the first sound in the array
-- Ps: if it doesn't work/it crashes, try putting "1" instead of "0"

function triggerButton()
	if soundStarted == false then
		playSound(soundNumber)
		soundNumber = soundNumber + 1 -- Automatically increase the number of the sound, so you don't need a paper full of annoying functions
		soundStarted = true -- The sound is started
	else
		stopSound(soundNumber) -- Stop the previously song, because the variable have been update on sound start
		soundStarted = false
	end
end

function playSound(int_number)
	for i, sound in pairs (sounds) do
		if i == int_number then
			game.YourPathToYourSounds:FindFirstChild(sound):Play()
		end
	end
end

function stopSound(int_number)
	for i, sound in pairs (sounds) do
		if i == int_number - 1 then
			game.YourPathToYourSounds:FindFirstChild(sound):Stop()
		end
	end
end

script.Parent.ClickDetector.MouseClick:connect(triggerButton)

It's something like this!

2 comments

0

the thing is it all really confuses me. Is there any more simple script?

0

In my opinion yes but it is toooooooooooo long

local sounds = {}

for i, v in pairs(script.Parent:GetChildren) do
	if v:IsA("Sound") then
		table.insert(sounds, (#sounds+1), v)
	end
end

local soundPlaying = false
local soundNumber = 0
local db=false


function toggleSounds()
if not db then
db=true
    if soundStarted == false then
	soundPlaying=true
	if soundNumber<#sounds then
        	soundNumber = soundNumber + 1
	else
		soundNumber=1 
	end
	sounds[soundNumber]:Play()
    else
        soundPlaying = false
	sounds[soundNumber]:Pause()
    end
db=false
end
end


script.Parent.ClickDetector.MouseClick:connect(toggleSounds)

You would just have to add a variable, on and off for this case.

local on = false
script.Parent.ClickDetector.MouseClick:connect(function()
	if on == false then
		on = true
		while true do
			        script.Parent.Parent.Sound1:Play()
        			wait(120)
     				script.Parent.Parent.Sound1:Stop()
        			wait(.1)
        			script.Parent.Parent.Sound2:Play()
        			wait(120)
        			script.Parent.Parent.Sound2:Stop()
       				wait(.1)
        			script.Parent.Parent.Sound3:Play()
        			wait(120)
       				script.Parent.Parent.Sound3:Stop()
        			wait(.1)
        			script.Parent.Parent.Sound4:Play()
        			wait(120)
        			script.Parent.Parent.Sound4:Stop()
        			wait(.1)
        			script.Parent.Parent.Sound5:Play()
       				wait(120)
       			 	script.Parent.Parent.Sound5:Stop()
        			wait(.1)
        			script.Parent.Parent.Sound6:Play()
       				wait(120)
        			script.Parent.Parent.Sound6:Stop()
        			wait(.1)
		end
	elseif on == true then
		on = false
		for i,v in pairs(script.Parent:GetChildren()) do
			if v:IsA("Sound") then
				v:Stop()
			end
		end
	end
end)

Just add a variable.

clicked = false

function onClick()
	if clicked == false then
		clicked = true
	else
		clicked = false
	end
end

while clicked == true do
	script.Parent.Parent.Sound1:Play()
	wait(120)
	script.Parent.Parent.Sound1:Stop()
	wait(.1)
	script.Parent.Parent.Sound2:Play()
	wait(120)
	script.Parent.Parent.Sound2:Stop()
	wait(.1)
	script.Parent.Parent.Sound3:Play()
	wait(120)
	script.Parent.Parent.Sound3:Stop()
	wait(.1)
	script.Parent.Parent.Sound4:Play()
	wait(120)
	script.Parent.Parent.Sound4:Stop()
	wait(.1)
	script.Parent.Parent.Sound5:Play()
	wait(120)
	script.Parent.Parent.Sound5:Stop()
	wait(.1)
	script.Parent.Parent.Sound6:Play()
	wait(120)
	script.Parent.Parent.Sound6:Stop()
	wait(.1)
end

The while loop should start up when clicked == true and stop when clicked == false.

--MightyWanderer

1 comment

0

This never connects to the click, won't work

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