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

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

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!

2 comments

0

No clue why the Code didn't inline.. First time it has messed up that bad... Yikes..

0

See help, to know how to properly use code blocks: https://scriptinghelpers.org/help/how-post-good-questions-answers

1 answer

✓ Accepted answer

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)

2 comments

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.

0

I edited my answer

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