ERROR | "For" loop not being executed

- Introduction! -

Hey there, developers! :wave:t3:
I am currently working on a Hide and Seek game but, once I started playtesting to try out my Loading Screen GUI to see If It was working properly before actually making It 100% functional, the for loop does not work.


- The Issue :thinking: -

After I started play-testing and noticed the error, I opened up the Output to check for any error messages, but, It didn’t print out any. Probably because there are no severe mistakes in the code.

The script below is the one I’m still making to set the Loading GUI as completely function.

---// Roblox Services
local ContentProvider_Service = game:GetService("ContentProvider")

---// Variables
local loadingBackgroundImage = script.Parent
local delayBackgroundImage = script.Parent.Parent:WaitForChild("DelayBackgroundImage")

local experienceAssets = game:GetDescendants()
local totalAssetsCount = #experienceAssets

---- UI Assets
local titleHolderFrame = loadingBackgroundImage:WaitForChild("TitleHolderFrame")
local loadingLabel = loadingBackgroundImage:WaitForChild("LoadingLabel")
local roblox2017Logo = loadingBackgroundImage:WaitForChild("Roblox2017Logo")

local experienceTitleLabel = titleHolderFrame:WaitForChild("ExperienceTitleLabel")
experienceTitleLabel.Text = game.Name

local progressIndicatorBar = loadingLabel:WaitForChild("ProgressIndicatorBar")
local progressIndicatorLabel = loadingLabel:WaitForChild("ProgressIndicatorLabel")

---// Controls the Loading UI
while true do
	wait(0.01)
	roblox2017Logo.Rotation += 1
end

for loadedAssetsCount, asset in pairs(totalAssetsCount) do
	progressIndicatorLabel.Text = "Assets Loaded: " ..loadedAssetsCount.. " / " ..totalAssetsCount
	progressIndicatorBar.Size = UDim2.new(loadedAssetsCount / totalAssetsCount, 0, 0.05, 0)
	ContentProvider_Service:PreloadAsync(asset)
end

if game:IsLoaded() then
	game.Loaded:Wait()
end

// Error Video //

I decided to record a video with the built-in Roblox Studio video recorder to show what’s wrong.

robloxapp-20230604-1117448.wmv (497.6 KB)


- Outro -

Thank you for your time reading! I hope I can get some help from our fellow developers! :slight_smile:

1 Like

Whoops, nevermind. After a second check on the code, I noticed that the line number 31 had a writing mistake.
BEFORE:

ContentProvider_Service:PreloadAsync(asset)

AFTER:

ContentProvider_Service:PreloadAsync({asset})

EDIT: Scratch that. It’s still not working.

EDIT 2: I’ve noticed another error. It’s not the totalAssetsCount, but yes the experienceAssets. Maybe the typing suggestions thingy for variables made me insert the wrong stuff. But even though after fixing it, nothing changed.

while true do
	wait(0.01)
	roblox2017Logo.Rotation += 1
end

This will never end…

Either create an ending condition for the while loop, or if it needs to run continuously spawn it in a separate thread. If you need to yield it in the future, spawn it inside a named variable.

It is actually supposed to run infinitely. I wonder If there is any way to add an ending condition, or If I run a repeat loop.

Use

task.spawn(function()
 --while loop code
end)

It creates a separate thread, which allows the for loop code to run

1 Like

Alright. If this works, I’ll make sure to mark your answer as solution. Give me some time to write this down and test it.

Perfect! It works incredibly well! Thank you a lot for your help! :smiley:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.