For loop never runs?

hey,

im making an obby game but the for loop never runs even though there is a valid folder in workspace and it has the correct name and all.

ive even rewrote the for loop by hand and it still doesnt work.

local player = game.Players.LocalPlayer

print('HIYA')

local function togglePlatformVisibility(platform, visible)
	if platform:IsA("BasePart") then
		print('ooo its a basepart')
		platform.Transparency = visible and 0 or 1
		platform.CanCollide = visible
		print('DONE!')
	end
end

print('yipeee')

local function handlePlatform(platform)
	local config = platform:FindFirstChild("Settings")
	if not config then
		warn("Platform " .. platform.Name .. " is missing a Settings Configuration!")
		return
	end

	print('heuyeyey')

	local disappearingTime = config:GetAttribute("disappearingTime") or 3 --default
	local reappearingTime = config:GetAttribute("reappearingTime") or 5 --default

	task.spawn(function()
		print("spawned func")
		while true do
			togglePlatformVisibility(platform, true)
			task.wait(reappearingTime)

			togglePlatformVisibility(platform, false)
			task.wait(disappearingTime)
		end
	end)
end

print('done with both functions!!!11')

local fadingPlatformsFolder = workspace:WaitForChild("FadingPlatforms", 5)
if fadingPlatformsFolder then
	print('found the folder lmao')
	for i, v in pairs(fadingPlatformsFolder:GetChildren()) do
		print("HEY")
		if v:IsA("BasePart") then
			print('omg its a basepart')
			handlePlatform(v)
			print("SENT FUNCTION WOOOOOOOO")
		end
	end
else
	warn("FadingPlatforms folder not found in workspace!")
end

(for loop near the end)

it prints “found the folder lmao” and nothing past that

I doubt there are any, but did any errors pop up in the console?

Running a for loop on :GetChildren() will always iterate once for every item which is a child to the original item. If there are no children, the loop will not iterate.

Ensure there are children under the folder.

1 Like

Probably unneccessary to use WaitForChild on a folder. You should have it wait until there are descendents of the folder though.

For example

if #fadingPlatformsFolder:GetChildren() == 0 then 
    fadingPlatformsFolder.ChildAdded:Wait() 
end

note that the waiting thing will be a bit more complicated if there are non-baseparts contained within the folder as well.

No errors in the console at all.

Jsjsjsjs

I can confirm that there is a part in the for loop.

Try switching the last lines of code with this.
This should print all children right after you get the Folder.

local fadingPlatformsFolder = workspace:WaitForChild("FadingPlatforms", 5)
print(fadingPlatformsFolder:GetChildren())
if fadingPlatformsFolder then
	print('found the folder lmao')
	for i, v in ipairs(fadingPlatformsFolder:GetChildren()) do
		print("HEY")
		if v:IsA("BasePart") then
			print('omg its a basepart')
			handlePlatform(v)
			print("SENT FUNCTION WOOOOOOOO")
		end
	end
else
	warn("FadingPlatforms folder not found in workspace!")
end

This script is run on local. It’s likely that the game haven’t loaded yet especially if you have streaming enabled. You should use .ChildAdded event.

Try to put this line of code under the loop

fadingPlatformsFolder.ChildAdded:Connect(handlePlatform)