Hello Everyone! It sure has been a while since I last posted on here lol.
I am currently trying to load a custom character upon the player joining. It’s been a while since I did Roblox Game Development so bear with me if this seems like a simple fix but this has been a really stubborn issue.
The issue, in question, is that when I try to load the character, the character does load for a split second, but then it resets back to the default character before the change. The code that attempts to accomplish changing the Character is listed below:
-- Needed modules --
local Character = require(script:FindFirstAncestor("ServerScriptService").Modules.CharacterModules.Character)
local CharacterModel = require(script:FindFirstAncestor("ServerScriptService").Modules.CharacterModules.CharacterModel)
game.Players.PlayerAdded:Connect(function(player)
local model = CharacterModel.new("DefaultCharacter")
local character = Character.new(player, model)
character:loadCharacter(game:GetService("ServerStorage"):WaitForChild("CharacterModels"))
end)
local InstanceLoader = require(script.Parent.Parent.UtilityModules.InstanceLoader)
local Character = {}
Character.__index = Character
-- Instantiates a new character that the player can use. characterModel should be of type CharacterModel
-- which comes from another module. player is the Player the character will be applied to.
function Character.new(player, characterModel)
local character = {}
setmetatable(character, Character)
character.__player = player
character.__characterModel = characterModel
return character
end
function Character:getPlayer()
return self.__player
end
function Character:getCharacterModel()
return self.__characterModel
end
function Character:setCharacterModel(newModel)
self.__characterModel = newModel
end
-- For characterContainer, input should be where the Character is located. --
function Character:loadCharacter(characterContainer)
local charModel = InstanceLoader.loadItem(characterContainer, self:getCharacterModel():getModel())
if charModel then
local plr = self:getPlayer()
charModel.Parent = workspace
plr.Character = charModel
local humanoid = charModel:FindFirstChild("Humanoid")
if humanoid then
humanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, false)
humanoid.BreakJointsOnDeath = false
end
plr:LoadCharacter()
else
warn("Character not found.")
end
print("Loaded Character.")
end
return Character
local CharacterModel = {}
CharacterModel.__index = CharacterModel
function CharacterModel.new(model)
local newCharacterModel = {}
newCharacterModel.__model = model
setmetatable(newCharacterModel, CharacterModel)
return newCharacterModel
end
function CharacterModel:getModel()
return self.__model
end
function CharacterModel:setModel(newModel)
self.__model = newModel
end
return CharacterModel
local InstanceLoader = {}
InstanceLoader.__index = InstanceLoader
function InstanceLoader.loadItem(container, item)
local instance = container:WaitForChild(item):Clone()
if instance then
return instance
else
warn(item.." not found inside "..container.Name)
return nil
end
end
return InstanceLoader
I mainly used module scripts as I find OOP to be more efficient. As I mentioned the character does load but only briefly, the issue is that after the brief appearance of the correct character, the player ends up resetting and going back to their default character. If anyone know’s how one can get around this issue, I would really appreciate if you could give me some advice.