GUI stuck in a loop?

Hi Devforum users, I’m trying to code a loading screen character selection, but it seems that when i select a character, the script loops again and I never get off the loading screen.

The reason for this: The character is not currently in workspace when cloned, so when I set it’s parent to workspace, it thinks the player is loading in again. I don’t know how to fix it, please help me guys!

---Server Sided script
local hasChosenCharacter = false

local function onCharacterAdded(character)
	if hasChosenCharacter then return end
	hasChosenCharacter = true

	tweenA:Play()
	tweenB:Play()
	task.wait(1)

	local function setupHover(button)
		button.MouseEnter:Connect(function()
			tweenSize(button)
			hover()
		end)
		button.MouseLeave:Connect(function()
			tweenSizeRegular(button)
			task.wait(0.2)
		end)
	end

	setupHover(marley)
	setupHover(cocoa)
	setupHover(ghost)
	setupHover(mango)

	for _,v in pairs(script.Parent:GetDescendants()) do
		if v:IsA("ImageButton") then
			v.MouseButton1Click:Connect(function()
				if character:FindFirstChild("Humanoid") and character.Humanoid.Health > 0 and not deb then
					deb = true
					local characterName = v.Parent.Name
					event:FireServer(characterName)
					selectionGui:Destroy()
					wait(1)
					-- No need to allow reuse of the GUI
				end
			end)
		end
	end
end


local hasHandledCharacter = false

local function handleCharacter(character)
	if hasHandledCharacter then return end
	hasHandledCharacter = true
	--print("Character handled:", character)
	onCharacterAdded(character)
end

if player.Character then
	task.defer(function()
		onCharacterAdded(player.Character)
	end)
else
	player.CharacterAdded:Once(onCharacterAdded)
end
---Local script

local rs = game:GetService("ReplicatedStorage")
local event = rs:WaitForChild("Remotes").Character
local ss = game:GetService("ServerStorage")
local characterFolder = ss:WaitForChild("CatCharacters")

event.OnServerEvent:Once(function(player, characterName)
	if not characterName then
		warn("characterName is nil!")
		return
	end
	print("Received characterName:", characterName)
	local ChosenCharacter = characterFolder:FindFirstChild(characterName)
	if not ChosenCharacter then
		warn("Character not found in folder:", characterName)
		return
	end
	ChosenCharacter = ChosenCharacter:Clone()

	player.Character = ChosenCharacter
	task.wait(1)
	ChosenCharacter = player.Name
	
end)

Apparently it works after a second time of clicking a character? This could be an easy fix and im just a bad scripter lol

Hey! The reason your loading screen loops is because when you set player.Character = ClonedCharacter, Roblox thinks the player is respawning, so it fires CharacterAdded again which triggers your selection logic all over.

I would add an attribute to your cloned character to let your script know it’s already a chosen one.

Where you clone the character:

ChosenCharacter = ChosenCharacter:Clone()
ChosenCharacter:SetAttribute("CustomCharacter", true)
ChosenCharacter.Name = player.Name
player.Character = ChosenCharacter

Then you would add the attribute handling in your onCharacterAdded function:

local function onCharacterAdded(character)
	if character:GetAttribute("CustomCharacter") then return end
	if hasChosenCharacter then return end
	hasChosenCharacter = true

	-- Etc...
end

Also, you had this line:
ChosenCharacter = player.Name

Don’t assign a string to the character. You probably meant:
ChosenCharacter.Name = player.Name

Let me know if this works.

1 Like

It does work, but also I needed to add this to the beginning. I also never used attributes before, so thank you for helping me learn!

	if character:GetAttribute("CustomCharacter") then
		selectionGui:Destroy()
	return end

(time to do homework now after wasting time) :skull_and_crossbones::skull_and_crossbones:

No problem at all, glad it worked!

Yeah attributes are super useful for stuff like this. Makes state tracking way easier.

Good luck with that homework :sweat_smile:

1 Like