My script runs too early, what solutions are there?

I have a script which runs whenever a new character is created, but in its current state, it produces errors when the character respawns. (although not when they first join the game) Through testing, I learned that the script is running to early, which is what causes it to break. I am using characterAppearanceLoaded and hasAppearanceLoaded to detect when to run the script, and I suspect this is why it breaks. Am I correct about this, and if so, what other methods can I use to detect when the character is fully loaded?

Hello, did you try using the PlayerAdded function instead?

You can wait for certain key parts to load using WaitForChild(). However, .characterAppearanceLoaded should work just fine. Can we see how you are incorporating it?

Here is how I’m using it. Let me know if you see a problem.

if player:HasAppearanceLoaded() == true then
	(run the script)
else
	player.CharacterAppearanceLoaded:Connect(function()
		(run the script)
	end)
end

Could we see what error you are getting in the output, do we can pinpoint he problem?

There is a loadAnimation inside the function, and the error is “LoadAnimation requires the Humanoid object to be a descendant of the game object”. When I add a wait, it seems to fix the error, but I don’t want to use that solution for obvious reasons.

You should use Character:WaitForChild("Humanoid") to wait for the Humanoid to exist. Then pass what WaitForChild() returns, into the LoadAnimation.

local HumanoidRootPart = Character:WaitForChild("Humanoid") 

Humanoid:LoadAnimation("animation")

Is this a local or server script?

It is a server script. (30 chars)

Just because the character is created doesn’t mean it has been parented to the Workspace. You could use character.AncestryChanged:Wait() at the top of your CharacterAppearanceLoaded event (it passes the character as an argument, use it) or you can use a while loop with a wait:

while character.Parent ~= workspace do
    RunService.Heartbeat:Wait();  --Assuming you already defined RunService
end
1 Like

I’ve actually already tried that. The problem is not that the humanoid doesn’t exist, it does. It’s just not a descendant of the game object. Thanks anyways!

That’s what “not a descendant of the game” means. You tried to access it, but it did not exist in the game space yet.

Have you tried yielding with characteradded?

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

That’s exactly what “not a descendant of the game” means. The OP already holds a reference to the Humanoid, but it hasn’t been parented to the game yet. Read the error carefully or you may end up giving the wrong solution again.

The character already exists (it’s passed through CharacterAppearanceLoaded), but it hasn’t been parented to the game yet. OP needs to yield using AncestryChanged or a while loop to check the character’s parent.

I see. Well its a bit unorthodox, but like your script, I usually just yield until the child im waiting for loads.

repeat wait() until character.Humanoid -- Or the child waiting for
local Plr = game:GetService("Players").LocalPlayer;
local Char = Plr.Character or Plr.CharacterAdded:Wait();
local Human = Char:WaitForChild("Humanoid");

--<Your Code


Plr.CharacterAdded:Connect(function(Character)
    Char = Character; Human = Character:WaitForChild("Humanoid");
--[[
Update the old "Char" variable, since it technically doesn't exist since a
new character is created whenever you respawn
]]
end);

The thing is the child already exists (OP also has reference to the Humanoid object), but calling :LoadAnimation requires the Humanoid to be a descendant of the game. In this case, the character has not been parented to the game yet.

Then couldnt you just yield until child.Parent == workspace. OP, have you tried any of wow’s code?

character:WaitForChild(“Humanoid”) would not help in this case because it would still return the Humanoid since it already exists.
Let me repeat: OP. Already. Has. The. Humanoid.
The issue here is that the character is parented to nil.
Why, you ask?
Because Roblox’s character loading flow sucks. For some reason, all of the character-related events fire BEFORE the character is inserted into the workspace, but after all of the essential parts of the character are created.

1 Like