Hello! I have a game that uses custom characters instead of Roblox avatars.
However, the character loading system seems a little unreliable to me.
The Code:
local ChangeChar = function(player,charModel)
player:LoadCharacter()
local currentChar = player.Character
local pos = currentChar.PrimaryPart.CFrame
local animate = charModel:FindFirstChild("Animate")
local health = charModel:FindFirstChild("Health")
local curAnimate = currentChar:FindFirstChild("Animate")
local curHealth = currentChar:FindFirstChild("Health")
if not health and curHealth then
curHealth:Clone().Parent = charModel
curHealth:Destroy()
end
charModel.Name = player.Name
charModel.Parent = workspace
player.Character = charModel
charModel.PrimaryPart:SetNetworkOwner(player)
charModel.PrimaryPart.CFrame = pos
if not animate and curAnimate then
curAnimate:Clone().Parent = charModel
curAnimate:Destroy()
end
if not charModel.Humanoid:FindFirstChildOfClass("Animator") then
local animator = new("Animator")
animator.Parent = charModel.Humanoid
end
currentChar:Destroy()
for _,v in pairs(starterCharScripts:GetChildren()) do
if not charModel:FindFirstChild(v.Name) then
v:Clone().Parent = charModel
end
end
ChangeCamSubject:FireClient(player,charModel.Humanoid)
charModel.Humanoid.Died:Connect(function()
wait(game.Players.RespawnTime)
updateChar(player)
end)
end
updateChar = function(player)
local equippedChar = player.EquippedCharacter.Value
local chars = player.Characters
local curChar = player.Character or player.CharacterAdded:Wait()
local changing = player.ChangingChar
if changing.Value then
repeat wait(0.1) until not changing.Value
end
changing.Value = true
if chars:FindFirstChild(equippedChar) then
local module = game.ServerStorage.Characters:FindFirstChild(equippedChar)
if module then
local char = module:FindFirstChildOfClass("Model")
ChangeChar(player,char:Clone())
else
player:LoadCharacter()
end
else
player:LoadCharacter()
end
wait(0.1)
changing.Value = false
end
If you have suggestions, please let me know! Thanks, WE
P.S. This code was written in August.