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

How can I GetChildren of a Folder everytime a new Child is put in?

Asked by 3 years ago
Edited by JesseSong 3 years ago

Please encode Lua code in the Lua block code tag (look for the Lua icon in the editor).

I am trying to do GetChildren() on a folder, but it comes back as nothing. The folder constantly has Sounds going in and out, so how would I go about trying to make it find the Child every time a new Sound is added into the folder? I already tried combining the scripts, but it was a mess.

Here is the script that works with the Folder, it puts the cloned Sound into the Current Song Folder:

local starterGui = game:GetService("StarterGui")
local shuffleBtn = script.Parent

local musicFolder = starterGui.MusicGui:WaitForChild("MusicFolder")
local songs = musicFolder:GetChildren()

local currentSongFolder = starterGui.MusicGui:WaitForChild("CurrentSongFolder")

shuffleBtn.MouseButton1Click:Connect(function()
    currentSongFolder:ClearAllChildren()
    wait(0.5)
    local chosenSong = songs[math.random(1, #songs)]

    local songClone = chosenSong:Clone()
    songClone.Parent = songClone
    songClone:Play()
end)

And here is the code where I am trying to make it so when a new Sound is added, it will look to see if a Child was Added to the Current Song folder:

local starterGui = game:GetService("StarterGui")
local sliderBG = script.Parent

local slider = sliderBG:WaitForChild("Slider")
local currentSongFolder = starterGui.MusicGui:WaitForChild("CurrentSongFolder")
local currentSong = currentSongFolder:GetChildren()

local mouse = game.Players.LocalPlayer:GetMouse()

local snapAmount = 1
local pixelsFromEdge = 1

local movingSlider = false


slider.MouseButton1Down:Connect(function()

    movingSlider = true
end)

slider.MouseButton1Up:Connect(function()

    movingSlider = false
end)
mouse.Button1Up:Connect(function()

    movingSlider = false
end)


mouse.Move:Connect(function()

    if movingSlider then

        local xOffset = math.floor((mouse.X - sliderBG.AbsolutePosition.X) / snapAmount + 0.5) * snapAmount
        local xOffsetClamped = math.clamp(xOffset, pixelsFromEdge, sliderBG.AbsoluteSize.X - pixelsFromEdge)

        local sliderPosNew = UDim2.new(0, xOffsetClamped, slider.Position.Y)

        slider.Position = sliderPosNew

        local roundedAbsSize = math.floor(sliderBG.AbsoluteSize.X / snapAmount + 0.5) * snapAmount
        local roundedOffsetClamped = math.floor(xOffsetClamped / snapAmount + 0.5) * snapAmount

        local sliderValue = roundedOffsetClamped / roundedAbsSize

        currentSong.Volume = sliderValue
    end
end)


Re-edited by JesseSONG!

0
No clue why the Code didn't inline.. First time it has messed up that bad... Yikes.. XRangerGuyX 29 — 3y
0
See help, to know how to properly use code blocks: https://scriptinghelpers.org/help/how-post-good-questions-answers JesseSong 3916 — 3y

1 answer

Log in to vote
1
Answered by
WoTrox 345 Moderation Voter
3 years ago
Edited 3 years ago

You can detect if a child is added with this function

musicFolder.ChildAdded:Connect(function(child)
    --You can use the "child" as you want
end)

Also:

songClone.Parent = songClone --In this line you're parenting the songClone to itself

Edit: To use it outside of the function, you can create and empty variable, then just assign the child to it:

local song --you can call it anything you want, but leave it empty

musicFolder.ChildAdded:Connect(function(child)
    song = child --now you can use it anywhere
end)
0
Oh! I didn't even realize. Been re-writing so much, it's been messing me up. So I tried to do Child Added earlier, but the problem is, if I want to call that Child that was added, how would I access it outside of the function? Because if I made a local variable in the function, it is only accessible in there. XRangerGuyX 29 — 3y
0
I edited my answer WoTrox 345 — 3y
Ad

Answer this question