What would be the best way to load a custom character into a player?

Right now I am currently trying to make a system that finds what type of character you are and then loads the custom character model into the player.

This is what i currently have:

function loadChar(player,CharacterType)
	local char = game.ServerStorage.CharacterModels:FindFirstChild(CharacterType)
	char.Name = player.Name

	local humanoid
	for i, child in pairs(char:GetChildren()) do
		if child.ClassName == "Humanoid" then
			humanoid = child
			break
		end
	end

	if not humanoid then
		humanoid = Instance.new("Humanoid", char)
	end

	player.Character = char
	char.Parent = game.Workspace
end

This function is called after the character is loaded, CharacterType would be the character that they have saved in data stores.

It does “work” however I am not happy with how it looks, what would be a better way of doing this?

2 Likes

This Is the best way you can use, Also Set The New Character Position In Workspace

If I’m not mistaken, you could use

:FindFirstChild(CharacterType,true)

Which does the same thing as your for i,v in pairs() loop. The true parameter in FindFirstChild is a boolean for recursive.

Also, I’m not quite sure because I can’t see the rest of the script, but its possible when declaring ‘char’, you might want to make a clone? of course this depends on how the rest of your system works.

hehe thanks, i completely forgot to call :clone()

1 Like
local Humanoid = Character:FindFirstChildWhichIsA("Humanoid") or Instance.new("Humanoid", Character)
local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
Humanoid = Humanoid or Instance.new("Humanoid", Character)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.