Hi!
I’m having some trouble with creating a custom character for my game. I already made a system to replace the current character (just putting a model called StarterCharacter inside of PlayerScripts and it automatically overrides the default) but now i’m trying to make my game more oop and make the character an object that I can easily swap out parts for. I am doing it the exact same way as before, but It doesn’t seem to work for some reason.
--calling main
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
--object module
local ship = require(script.Parent:WaitForChild("shipObject"))
local function initializePlayer(user)
--creating the player ship in game
local newPlayer = ship:newPlayerShip(user)
end
initializePlayer(player)
--this is where I hold the information for each ship model
local shipFolder = game.ReplicatedStorage.shipModels
local shipInventory = {
--1
["Galleon1"] = {
["Model"] = shipFolder.StarterGalleon,
["Name"] = "StarterCharacter",
},
--2
["Galleon2"] = {
["Model"] = shipFolder.TestGalleon,
["Name"] = "StarterCharacter"
},
}
return shipInventory
--creating the player object
local replicatedStorage = game:GetService("ReplicatedStorage")
local shipInventory = require(replicatedStorage:WaitForChild("shipInventory"))
local shipObject = {}
local shipMethods = {}
local shipMetatable = {__index = shipMethods}
function shipObject:newPlayerShip(user)
local playerShip = shipInventory["Galleon1"]
local character = user.Character or user.CharacterAdded:Wait()
local newship = {}
--getting the ship model from inventory
newship.Model = playerShip.Model:Clone()
--inserting every model part inside of StarterCharacter
for i, v in pairs(newship.Model:GetChildren())do
v.Parent = game.StarterPlayer.StarterCharacter
end
setmetatable(newship, shipMetatable)
return newship
end
return shipObject
--server side ship behavior
-- game.Players.CharacterAutoLoads = false
local function onCharachterAdded(character)
local rootPart = character:WaitForChild("HumanoidRootPart")
while not rootPart:IsDescendantOf(workspace) do
wait()
end
rootPart:SetNetworkOwner(game.Players:GetPlayerFromCharacter(character))
end
local function onPlayerAdded(player)
player.CharacterAdded:Connect(onCharachterAdded)
end
game.Players.PlayerAdded:Connect(onPlayerAdded)
Now, the code above does not error and it does insert all the ship parts inside of the ship model that I have in RS into it, but the player does not get copied into the game.
First image is before I start the game, and second image is after I start the game.
Now usually if I just were to put StarterCharacter with everything inside, it would work. But now this is whats going on. The player appears for a couple seconds with none of the parts but the humanoidRootPart and then deletes itself.
(gif)
Don’t exactly know how to go about this now, any help would be appreciated