Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is this sound invincible?

Asked by
itsJooJoo 195
8 years ago

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

Log in to vote
0
Answered by 8 years ago

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.

0
I put all the ends at the bottom of the script. Is this really the problem? itsJooJoo 195 — 8y
0
omigosh I am such an idiot. Thank you so much! itsJooJoo 195 — 8y
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. darkelementallord 686 — 8y
0
Or it may continue. darkelementallord 686 — 8y
Ad
Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

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!

Answer this question