How detect when a character is fully loaded?

Every time I want to ensure a character is fully loaded I do a repeat loop as follows:

repeat wait() until player.Character

This method works well but it is my understanding that this is bad practice, I read a post about using bindable events to do this, I just don’t know how to go about it.

CharacterAdded is supposed to fire when the character is loaded, or so I am told, but it doesn’t work like that for a specific script I am working on.

The script in question:

game.Players.PlayerAdded:Connect(function(player)
	repeat wait() until player.Character
	swordModule.updateSword(player) --A function in a module which welds the sword and whatnot
	player.CharacterAdded:Connect(function(char)
		repeat wait() until player.Character
		swordModule.updateSword(player)
	end)
end)

If I don’t add the repeat loop, in this case, the sword will not weld to the hand the reason being, I assume, is that the character is not fully loaded. I tested this theory by adding a wait() instead of the repeat loop and that also worked, I replaced the wait() with the loop to satisfy the uncertainty of loading times between users.

Essentially I want to know if there is a way that is better practice which I can implement instead of using a loop.

11 Likes

To look for the player’s character the first time is relatively simple and doesn’t require you to use repeat wait() etc. Simply replace that line with this:

if not player.Character then player.CharacterAdded:Wait() end

This will wait and check for the player’s character if it doesn’t exist yet. Using :Wait() after most Events will yield until it occurs, then it will proceed to the rest of the script.

You also wouldn’t need to use a check in the CharacterAdded function as the char variable you have in there should contain it already.


For future reference: If you wanted to set that to a variable, then you can use this line instead:

local char = player.Character or player.CharacterAdded:Wait()

The variable checks if the character is there; if not, then it’ll wait for one to be added. Using events with :Wait() will also return what their callback functions would normally return.

6 Likes

Thank you for the response,
The only problem is that CharacterAdded is not sufficient in this case which is the reason I did the repeat loop. While CharacterAdded does indicate the character has been added I do not believe it is fired when the character is fully loaded which consequentially breaks the weld that is being attempted in my script. My question is if there is another way to ensure the character is fully loaded.

1 Like

It’s not a good practice to use any sort of wait, instead you can use this.

game:GetService("Players").PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)
        swordModule.updateSword(Player)
    end
end

If you’re looking for the player to have some sort of object inside of his character then I know something else aswell.

2 Likes

Then I’m pretty sure you may wanna look at CharacterAppearanceLoaded and HasAppearanceLoaded if you’re concerned about certain parts being there.

33 Likes

Player.CharacterAppearanceLoaded fires when the character is “fully loaded”

7 Likes

Alright, I will check that out. That does seem to be what I was looking for, thank you.

3 Likes

It is good idea to check if the loading orb around the player has gone, also. I wrote a script for one player game, where the loading screen is played during the existence of the orb, but I am not sure if it will work for the multiplayer… Probably it should, I just had no chance to check it :slight_smile:

4 Likes