Why is this not working as I would expect?

I made a script for a loading screen and it doesn’t seem to work. It stays at zero percent and only seems to loop once. Can somebody explain what’s wrong?

game.ReplicatedFirst:RemoveDefaultLoadingScreen()
local player = game.Players.LocalPlayer
local loadCoro = coroutine.create(function()
	local percentage = 0
	while true do
		player:WaitForChild("PlayerGui").LoadingScreen.MainFrame.Text.Text = "Loading: " .. percentage .. "%"
		print(percentage/100)
		player:WaitForChild("PlayerGui"):WaitForChild("LoadingScreen").MainFrame.Background.Green.Size.X.Scale = percentage/100
		percentage+=1
		wait(.1)
	end
end)

local PlayerGui = game.Players.LocalPlayer:WaitForChild("PlayerGui")

local GUI = script.LoadingScreen:Clone()
GUI.Parent = PlayerGui

coroutine.resume(loadCoro)

Why are you using multitasking here? That seems unnecessary and overcomplicated.

I was doing that so that the loop wouldn’t interfere with anything in the future, but I’ll try it without the coroutine.

The loop should be done by the time the future comes around.

I fixed it so it wasn’t in a coroutine and it’s saying Scale cannot be assigned to for the size line

Is that an error? Did you try and make it a function?

Coroutines do not report errors to log, you are attempting to set the scale of a UI element to a number, it must be set via UDim2().

local ReplicatedFirst = game:GetService("ReplicatedFirst")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local playergui = player:WaitForChild("PlayerGui")
local loadscreen = playergui:WaitForChild("LoadingScreen")

ReplicatedFirst:RemoveDefaultLoadingScreen()

local loadCoro = coroutine.create(function()
	local percentage = 0
	while true do
		playergui.LoadingScreen.MainFrame.Text.Text = "Loading: " .. percentage .. "%"
		print(percentage/100)
		loadscreen.MainFrame.Background.Green.Size = UDim2.new(percentage/100, XPixels, YScale, YPixels)
		percentage+=1
		wait(0.1)
	end
end)

local GUI = script.LoadingScreen:Clone()
GUI.Parent = playergui

coroutine.resume(loadCoro)

I have also corrected some bad scripting practices.

You will need to change XPixels, YScale, etc. to what they are of your green bar.

1 Like

Yeah that’s most likely the answer. I don’t do UI so I would not notice that.

I fixed it because it wasn’t exactly working how you had it:

local ReplicatedFirst = game:GetService("ReplicatedFirst")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local playergui = player:WaitForChild("PlayerGui")
local loadscreen = script.LoadingScreen:Clone()

loadscreen.Parent = playergui

ReplicatedFirst:RemoveDefaultLoadingScreen()

local loadCoro = coroutine.create(function()
	local percentage = 0
	while true do
		playergui.LoadingScreen.MainFrame.Text.Text = "Loading: " .. percentage .. "%"
		print(percentage/100)
		loadscreen.MainFrame.Background.Green.Size = UDim2.new(percentage/100, 0, 1, 0)
		percentage+=1
		wait(0.1)
	end
end)


coroutine.resume(loadCoro)
1 Like