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

for loop for everything not working help?

Asked by
NotSoNorm 777 Moderation Voter
8 years ago

I have this for loop, it works for the most part but I have to spam it to work, it doesn't repeat for all the instances named "Sound"

x = game.Workspace.DJBoardPRO
Audios = game.Workspace.DJBoardPRO:GetChildren()

script.Parent.MouseButton1Down:connect(function()
    for i = 1, #Audios do
        if Audios[i].Name == "Sound" then
            x.Sound.Volume = "1"
            wait(0.05)
            x.Sound.Volume = "0.8"
            wait(0.05)
            x.Sound.Volume = "0.6"
            wait(0.05)
            x.Sound.Volume = "0.4"
            wait(0.05)
            x.Sound.Volume = "0.2"
            wait(0.05)
            x.Sound:Stop()
            wait(.1)
            x.Sound:Destroy()
        end
    end
end)

1 answer

Log in to vote
1
Answered by
davness 376 Moderation Voter
8 years ago

Let's get your errors:

1. Using a numeric loop instead of a generic loop

Using generic loops make you get all the contents of a table:

-- we don't need x value
local Audios = game.Workspace.DJBoardPRO:GetChildren()

script.Parent.MouseButton1Down:connect(function()
    for i,v in pairs(Audios) do
        if v.Name == "Sound" then
            v.Volume = "1"
            wait(0.05)
            v.Volume = "0.8"
            wait(0.05)
            v.Volume = "0.6"
            wait(0.05)
            v.Volume = "0.4"
            wait(0.05)
            v.Volume = "0.2"
            wait(0.05)
            v:Stop()
            wait(.1)
            v:Destroy()
        end
    end
end)

2. Strings for float value?

You're setting a float value with a string one, so, let's change it - probably the main cause of your error

local Audios = game.Workspace.DJBoardPRO:GetChildren()

script.Parent.MouseButton1Down:connect(function()
    for i,v in pairs(Audios) do
        if v.Name == "Sound" then
            v.Volume = 1
            wait(0.05)
            v.Volume = 0.8
            wait(0.05)
            v.Volume = 0.6
            wait(0.05)
            v.Volume = 0.4
            wait(0.05)
            v.Volume = 0.2
            wait(0.05)
            v:Stop()
            wait(.1)
            v:Destroy()
        end
    end
end)

3. Using a second for loop

Using another for loop cleans up your script and make it look more professional.

local Audios = game.Workspace.DJBoardPRO:GetChildren()

script.Parent.MouseButton1Down:connect(function()
    for i,v in pairs(Audios) do
        if v.Name == "Sound" then
        for i = 1,0,-0.2 do
            v.Volume = i
            wait() -- waits 1/30 second, but you may set it to 0.05
        end
            v:Stop()
            wait(.1)
            v:Destroy()
        end
    end
end)

4. Coroutines

If you want to make the sounds fade out at the same time, you may use a coroutine, else, ignore this step:

local Audios = game.Workspace.DJBoardPRO:GetChildren()

script.Parent.MouseButton1Down:connect(function()
    for i,v in pairs(Audios) do
        if v.Name == "Sound" then
        fadeOut = coroutine.create(function() -- creates a new thread, something like a ghost script
            for i = 1,0,-0.2 do
                v.Volume = i
                wait() -- waits 1/30 second
            end
                v:Stop()
                wait(.1)
                v:Destroy()
        end)
        coroutine.resume(fadeOut)
        end
    end
end)
0
Thanks dude, this helps a lot, saving for refrence NotSoNorm 777 — 8y
Ad

Answer this question