CharacterAdded Problem

CharacterAdded is delayed. I want to find the best way to fastest wait until found character

local Players = game:GetService("Players");

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		repeat task.wait() until Character ~= nil -- This line! :)
		-- print("Character exist");
	end)
end)

I have tried repeat task.wait() until Character ~= nil but… is there a better way to wait, like using any service?

(You can ask me more to explain the problem. Sorry for my bad grammar)

1 Like

You don’t need the repeat that’s unnecessary. That event will fire once the character has been added meaning the argument will already be the character immediately.

1 Like

I attempted to set the character’s parents, but it can’t, I need to use the script above.

What exactly are you trying to achieve with this script?

1 Like

Replace characteradded with

Player.CharacterAppearanceLoaded:Connect(function(Character)

end)
2 Likes

I want to set the character’s parents to workspace.Characters. But it can’t be done without using the script above. I want to know if anyone has a better way to wait for a character to fully load.

I tested and couldn’t set the parent…

Loop Method


game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		while wait() do
			if char:FindFirstChild("Humanoid") then
				char.Parent = workspace.Characters
				break
			end
		end
		
	end)
end)

Alternative method:


game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		repeat
			wait()
		until char:FindFirstChild("Humanoid"); char.Parent = workspace.Characters
	end)
end)
1 Like

It worked! Thank you. I’m waiting for more people to comment.

1 Like

Look out for practices that could yield smelly code!

local function onPlayerAdded(player)
    -- whatever things you'd like
    player.CharacterAdded:Connect(function(character)
        while not character:FindFirstChild("Humanoid") do
            task.wait()
        end

        character.Parent = workspace.Characters
    end)
end

for _, player in pairs(Players:GetPlayers()) do
    -- pesky loading time if the player somehow loaded faster than the server
    onPlayerAdded(player)
end

Players.PlayerAdded:Connect(onPlayerAdded)
1 Like

If the CharacterAdded is on the server, theres no need for you to wait. The CharacterAdded event waits for all character parts are loaded on the server.

2 Likes

Setting the characters parent when the character has just loaded is totally glitchy by the way, its necessary with a wait, from my testing the wait doesnt have to be big - I just did task.wait()

Hope this helps!

1 Like

You can wait a frame, but that doesn’t work 100% of the time.

The most reliable and efficient way I found was

if not character:IsDescendantOf(workspace) then
    character.AncestryChanged:Wait()
    character.Parent = workspace.Entities --// arbitrary folder
end
1 Like

If that works you can do this instead:

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		char:WaitForChild("Humanoid")
		char.Parent = workspace.Characters
	end)
end)

Also you should store the CharacterAdded event somewhere so you can :Disconnect it when the player leaves to avoid memory leaks.

1 Like

Oh… memory leaks? I forgot about this. Anyway, this post is about CharacterAdded delay. I’ll learn more!

Can it be used in every situation? ex. server delay or etc.

I’m not 100% sure but it works fine i think

1 Like