Gui is not working with loading data

So I have an egg hatching system that when you join the game and have pets loaded from your last session, it creates frames for each pet. It works on the server (when I rejoin the game the values are still loaded), but on the GUI, it doesn’t clone a frame.

Client Script:

local char = player.CharacterAdded:Wait()

if char ~= nil then
	print('a') --doesnt even print lmao
	local pets = player:WaitForChild("Pets")
	if #pets:GetChildren() >= 1 then
		for i, v in pairs(pets:GetChildren()) do
			_G.CreateTemplate(v.Name)
		end
	end
end

if it doesn’t even print then try this on the start

repeat 
wait() 
until game.Players.LocalPlayer.Character ~= nil
local char = game.Players.LocalPlayer.Character
local char = player.Character or player.CharacterAdded:Wait()

Remember that if the character already exists then you don’t need to wait until it’s added. If you don’t check if the player’s character already exists then the script will think you are waiting for a new character to be added.

@nikos300 also posted a solution, but his method is probably not the right way to go.

It’s also recommended to use task.wait() over wait

Well it prints now, but it still doesn’t work

It prints, but it still doesn’t clone the ui. Also, thank you for recommending task.wait(), I will definitely use it.

i edited the script use that now and remove the char you used

No problem. For your other issue perhaps it could be another loading problem. You may need to wait for the amount of pets to be greater than 1 before loading them.

If the client runs faster than the server it may just assume there’s no pets and move on.

Unfortunately, it still doesn’t work

any other errors? perhaps remove the check for if character is nil

Thanks, adding a repeat task.wait() helped.

1 Like

No problem. Also if you’re still having loading issues perhaps this could help you out. I have made a template for you that you can base the pets loading off of.

This is only if you’re creating the templates on the server not the client

local Bindable = game.ServerStorage.BindableEventName
local function assignPetTemplates(player, char)
	local pets = player:WaitForChild("Pets")
	if #pets:GetChildren() >= 1 then
		for i, v in pairs(pets:GetChildren()) do
			_G.CreateTemplate(v.Name)
		end
	end
end
Bindable.Event:Connect(assignPetTemplates)

local function PlayerAdded(Player)
	-- get pet data
	-- load pet data
	local function CharacterAdded(Character)
		Bindable:Fire(Player, Character)
	end
	-- In case our player's character already exists in the game before connecting our event
	if Player.Character then
		CharacterAdded(Player.Character)
	end
	Player.CharacterAdded:Connect(CharacterAdded)
end

-- In case players are already in the game before we connect our event
for _, Player in pairs(game.Players:GetPlayers()) do
	task.spawn(function()
		PlayerAdded(Player)
	end)
end
game.Players.PlayerAdded:Connect(PlayerAdded)

EDIT: Had to fix it up a bit