Functions for a Loading Screen not being executed without an apparent reason

What’s the issue here?

The main problem with this code is that some of the functions aren’t being executed when they’re called or when certain conditions are met. Respectively, they are animateHovering_SkipButton(), completeLoading(), and skipLoadingProcedure(). I’ve already tried double-checking the output for any possible errors or warnings but nothing can be seen (even with filters). Last thing I want to do here is scrap the code and re-write it from the ground up.


Main code

Yes, it’s long, but it’s organized. My biggest worry is the code not working.

--- Services
local ReplicatedFirst = game:GetService("ReplicatedFirst")
local StarterGUI = game:GetService("StarterGui")
local ContentProviderService = game:GetService("ContentProvider")
local _Workspace = game:GetService("Workspace")
local TweenService = game:GetService("TweenService")
local PlayersService = game:GetService("Players")

--- Variables
local player = PlayersService.LocalPlayer
local playerGUI = player.PlayerGui
local currentCamera = _Workspace:WaitForChild("Camera")

local camerasFolder = _Workspace:WaitForChild("CinematicCameras")
local totalCameras = camerasFolder:GetChildren()

local userInterfacesFolder = ReplicatedFirst:WaitForChild("UserInterfaces")

local loadingInterface = userInterfacesFolder:WaitForChild("LoadingUI")
loadingInterface.Parent = playerGUI

local cutsceneBarsFolder = loadingInterface:WaitForChild("CutsceneBars")
local topSceneBar = cutsceneBarsFolder:WaitForChild("TopCutsceneBar")
local bottomSceneBar = cutsceneBarsFolder:WaitForChild("BottomCutsceneBar")

local loadingLabel = loadingInterface:WaitForChild("LoadingLabel")
local assetPreloadingLabel = loadingInterface:WaitForChild("AssetPreloadingLabel")
local skipButtonAssetGroup = loadingInterface:WaitForChild("SkipButtonCanvas")
local fadingEffectFrame = loadingInterface:WaitForChild("LoadingFadingEffectFrame")

local skipButton = skipButtonAssetGroup:WaitForChild("SkipButton")
local hoverAnimationFrame = skipButtonAssetGroup:WaitForChild("AnimatedHoverFrame")

--- Constants
local TOTAL_ASSETS = game:GetDescendants()
local ASSETS_COUNT = #TOTAL_ASSETS
local TOTAL_LOADED_ASSETS = 0
local CURRENT_LOADING_PERCENTAGE  = 0

local LOADING_SKIP_TRIGGERED = false

local HOVER_ANIMATION_INFO = TweenInfo.new(0.75, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out)
local HOVER_ENDED_ANIMATION_INFO = TweenInfo.new(0.75, Enum.EasingStyle.Cubic, Enum.EasingDirection.In)

local FADE_IN_ANIMATION_INFO = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
local FADE_OUT_ANIMATION_INFO = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.In)

--- Changes the Loading Screen
ReplicatedFirst:RemoveDefaultLoadingScreen()

--- Skip button additional functionality
local function allowSkipButton(callType: boolean)
	print("A call was successful for allowSkipButton!")
	if typeof(callType) == "boolean" then
		print("typeof(...) check success!")
		if callType == true then
			print("callType: true! Setting the skip button to visible...")
			skipButtonAssetGroup.Visible = true
		elseif callType == false then
			print("callType: false! Setting the skip button to non-visible...")
			skipButtonAssetGroup.Visible = false
		end
	else
		warn("typeof(...) check failed! Invalid type for isHovering argument! Type: " ..typeof(callType))
	end
end

--- Function to animate the hovering functionality of the skip button
local function animateHovering_SkipButton(isHovering: boolean)
	print("A call was successful for animateHovering_SkipButton!")
	if typeof(isHovering) == "boolean" then
		print("typeof(...) check success!")
		if isHovering == true then
			print("isHovering: true! Creating and playing HOVER_TWEEN...")
			local HOVER_TWEEN = TweenService:Create(hoverAnimationFrame, HOVER_ANIMATION_INFO, { Size = UDim2.new(1, 0, 1, 0)})
			HOVER_TWEEN:Play()
		elseif isHovering == false then
			print("isHovering: false! Creating and playing HOVER_ENDED_TWEEN...")
			local HOVER_ENDED_TWEEN = TweenService:Create(hoverAnimationFrame, HOVER_ENDED_ANIMATION_INFO, { Size = UDim2.new(0, 0, 1, 0)})
			HOVER_ENDED_TWEEN:Play()
		end
	else
	warn("typeof(...) check failed! Invalid type for isHovering argument! Type: " ..typeof(isHovering))
	end
end

--- Function to finalize the loading process
local function completeLoading()
	print("A call was successful for completeLoading!")
	
	local FADE_IN_TWEEN  = TweenService:Create(fadingEffectFrame, FADE_IN_ANIMATION_INFO, { Transparency = 0 })
	local FADE_OUT_TWEEN = TweenService:Create(fadingEffectFrame, FADE_OUT_ANIMATION_INFO, { Transparency = 1})
	loadingLabel.Text = `LOA<stroke color="#000000" joins="miter" thickness="3">DED!</stroke>`
	
	task.wait(1)
	FADE_IN_TWEEN:Play()
	
	FADE_IN_TWEEN.Completed:Connect(function()
		topSceneBar.Visible = false
		bottomSceneBar.Visible = false
		loadingLabel.Visible = false
		assetPreloadingLabel.Visible = false
		task.wait(1.5)
		FADE_OUT_TWEEN:Play()
		FADE_OUT_TWEEN.Completed:Connect(function()
			loadingInterface:Destroy()
		end)
	end)
end

--- Function to setup the main loading procedure
for currentAsset, instance in pairs(TOTAL_ASSETS) do
	if TOTAL_LOADED_ASSETS == ASSETS_COUNT then
		print("The loading has been completed! Calling completeLoading...")
		completeLoading()
		break
	end
	
	if LOADING_SKIP_TRIGGERED == false then
		TOTAL_LOADED_ASSETS += 1
		CURRENT_LOADING_PERCENTAGE = math.floor((TOTAL_LOADED_ASSETS / ASSETS_COUNT) * 100)
		assetPreloadingLabel.Text = "Things loaded: " ..TOTAL_LOADED_ASSETS.. " out of " ..ASSETS_COUNT.. " (" ..CURRENT_LOADING_PERCENTAGE.. "%)"
		
		if CURRENT_LOADING_PERCENTAGE >= 45 and CURRENT_LOADING_PERCENTAGE < 75 then
			allowSkipButton(true)
		elseif CURRENT_LOADING_PERCENTAGE >= 75 then
			allowSkipButton(false)
		end
		
		ContentProviderService:PreloadAsync({instance})
		print("Asset preloaded successfully! - " ..instance.Name)
		task.wait(0.05) -- Delay to avoid CPU overload
	else
		break
	end
end

--- Function to skip the loading
local function skipLoadingProcedure()
	print("A call was successful for skipLoadingProcedure!")
	
	CURRENT_LOADING_PERCENTAGE = 100
	assetPreloadingLabel.Text = "Things loaded: " ..ASSETS_COUNT.. " out of " ..ASSETS_COUNT.. " (" ..CURRENT_LOADING_PERCENTAGE.. "%)"
	completeLoading()
end

--- Events listener
skipButton.MouseEnter:Connect(function()
	if skipButtonAssetGroup.Visible == true then
		animateHovering_SkipButton(true)
	end
end)

skipButton.MouseLeave:Connect(function()
	if skipButtonAssetGroup.Visible == true then
		animateHovering_SkipButton(false)
	end
end)

skipButton.MouseButton1Click:Connect(function()
	if skipButtonAssetGroup.Visible == true then
		LOADING_SKIP_TRIGGERED = true
		skipLoadingProcedure()
	end
end)

I’ll be waiting for a response. In the meantime, I’ll keep on trying to fix this. :wink:

Fix found!

I just had to spawn the for loop in a separate thread. It was blocking the code from running. Anyways.

task.spawn(function()
   --- for code went here instead of being set to the main thread.
end)

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