StarterGui script issues after disabling AutoLoadCharacter and then doing LoadCharacter

I am making a game that morphs players in and out of a morph as rounds go and end, and in order to do this so the player doesn’t automatically get switched back to their default character instead of the morph when I spawn them in with it I used a script to turn off CharacterAutoLoads when the player joins the game. But then once a round ends I use LoadCharacter on every player and everything breaks (the UI is still visible and all) as all scripts that update playerGui like the countdown break and say that what needs to be referenced is nil. I have tried spawning in all the Gui upon joining but maybe I need to do it once I use loadCharacter as it might already be spawned in?

Screenshot 2024-08-31 210512

image

1 Like

When you loadCharacter, the old scripts will, along with the old characters, get killed. Considering that they’re still running, they may error as their references are now broken.

You should destroy the player’s character and the scripts inside them which is used to create the GUI, and then copy over a new set of them.

If the scripts which handles the GUI are not within the character, you have to make sure that they update to the new character when the character is re-loaded.

For more specific help, it would be nice if you provided the scripts that was breaking, their hierarchy in the explorer, and information such as wether or not the character is destroyed before it is loaded again.

So basically to solve the problem, before I call LoadCharacter I need to destroy the current character to get rid of the killed scripts? Or no?

In the above image line 18 does not work as GetChildren is Nil

image

I would assume so, but gimme a second to read your actual script

Yep, that should be how it works. StarterGui loads things into the character, and if you call LoadCharacter again, the previous character is parented to nil, so Script.Parent would no longer be existant. (I assume)

So something like this?

Or am I missing something here?

Here’s the script in text if you want it:

function gameEndProcedure()
	debounce = true
	table.clear(_G.evilsTable)
	table.clear(_G.survivorTable)
	roundBool = false
	_G.EnemySelectorActivated = false
	for i, plr in pairs(players:GetChildren()) do
		local character = plr.Character or plr.CharacterAdded:Wait()
		
		table.insert(_G.survivorTable, plr.UserId)
		character:Destroy()
		plr:LoadCharacter()
		for _, ui in pairs(game.StarterGui:GetChildren()) do
			local u = ui:Clone()
			u.Parent = plr.PlayerGui
		end
	end
end

OH WAIT OH NO IM STUPID

StarterGUI copies to the PlayerGUI folder, not the character

In this case, simply update the BlockVal variable whenever the player respawns, as otherwise, it would still point towards the old one.

(To update it, just connect a function to CharacterLoaded, and set the BlockVal variable again)

Got it, Ill try it out and see what happens

You mean CharacterAdded right?

Oh yea, i forgot the name :sweat_smile:

aaaaaaa

Still problems unless I am being stupid:

(Please press “reply” under my posts instead of replying to your own post, or atleast ping me with @PANDASonNOOB , otherwise, I don’t get notified)

use an if statement to check if scrollFrame != nil before you get its children ig

Oh wait also
do you have StarterGui.ResetPlayerGuiOnSpawn set to true? If so, turn it off.

Ill do that, and I was experimenting with it trying to fix the problem but I turned it back off afterwards

1 Like

Oh yea, also, check to see how many version of the GUI exists in the player.PlayerGUI after they respawn.

WAIT NO
In your serverscript, don’t clone the ui to the PlayerGui unless you know they dont exist there. If there is already a set of ui there, don’t clone it again

wait actually, try doing PlayerGui.BlockPlacement.BlockVal:GetChildren() instead

Sorry went to sleep, but I’ll try this out. For whatever the reason when I tried out the previous solution of if blockVal ~= nil, it worked and the GUI still worked as it should for some reason? I couldn’t tell you