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

Why is this sound invincible?

So I am making a TextButton, when you press it, a party comes on and there's dubstep music and fireworks and a dance floor and all that stuff. There is another textbutton, when you click that, it removes it all. Here is the localscript:

function onClicked()
	local dancefloor = workspace:FindFirstChild('DanceFloor')
	if dancefloor ~= nil then dancefloor:Destroy()
	
	local colorcorrect = game.Lighting:FindFirstChild('ColorCorrection')
	if colorcorrect ~= nil then colorcorrect:Destroy()
	
	local hint = workspace:FindFirstChild('Message')
	if hint ~= nil then hint:Destroy()
	
	local gui = game.StarterGui:FindFirstChild('IlluminatiGui')
	if gui ~= nil then gui:Destroy()
	
	local sound = workspace:FindFirstChild('Dubstep')
	if sound ~= nil then	sound:Destroy()
					end
				end
			end
		end
	end
end

script.Parent.MouseButton1Down:connect(onClicked)

So all of the other stuff gets removed, however the Dubstep sound stays there. So really, this is the line of code I'm worried about

local sound = workspace:FindFirstChild('Dubstep')
if sound ~= nil then	sound:Destroy()

Yes, it is in Workspace. Yes, it works if I put the same line in the command bar. Yes, I am certain this works in both the game and studio. What's wrong with this script? Please help!

2 answers

✓ Accepted answer

You are forgetting your end s.

Every if statement requires an end to close it off, so:

if dancefloor ~= nil then
	dancefloor:Destroy()
end

I'd advise looking at the intro tutorials on the wiki. Maybe something like this to build your understanding.

4 comments

0

I put all the ends at the bottom of the script. Is this really the problem?

0

omigosh I am such an idiot. Thank you so much!

0

Well, if dancefloor doesn't exist, then nothing will happen. Consequently if colorcorrect doesn't exist, then nothing after that will happen. It may be your problem. Also with sounds, you'd need to do Sound:Stop() before you destroy it.

0

Or it may continue.

Tab your code correctly.

When you have "blocks" of code grouped in, e.g., between then and end in an if statement, you should indent them one level:

function onClicked()
	local dancefloor = workspace:FindFirstChild('DanceFloor')
	if dancefloor ~= nil then
		dancefloor:Destroy()
		local colorcorrect = game.Lighting:FindFirstChild('ColorCorrection')
		if colorcorrect ~= nil then
			colorcorrect:Destroy()
			local hint = workspace:FindFirstChild('Message')
			if hint ~= nil then
				hint:Destroy()
				local gui = game.StarterGui:FindFirstChild('IlluminatiGui')
				if gui ~= nil then
					gui:Destroy()
					local sound = workspace:FindFirstChild('Dubstep')
					if sound ~= nil then
						sound:Destroy()
					end
				end
			end
		end
	end
end

script.Parent.MouseButton1Down:connect(onClicked)

This gives you a much clearer understand of what happens when -- you can easily line-up the insides of an if.

In your case: You only sound:Destroy() when there's a dance floor, and, a ColorCorrect, and, a Message, etc.

Since you're repeating yourself so much, this is probably a good use for a function, too:

function delete(parent, name)
	local child = parent:FindFirstChild(name)
	if child then
		child:Destroy()
	end
end

your cleanup function now looks like

function cleanup()
	delete(workspace, "DanceFloor")
	delete(game.Lighting, "ColorCorrection")
	delete(workspace, "Message")
	delete(game.StarterGui, "IlluminatiGun")
	delete(workspace, "Dubstep")
end

Much more compact and understandable!

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