Hello, I am attempting to create a morph system where the player uses a proximity prompt to turn into a given model. With some help from the forums I made the following script:
script.Parent.ProximityPrompt.Triggered:Connect(function(player)
-- arguments for the player that is respawing and the model the player morphs into.
local model = game:GetService("Workspace").IShowSpeed --This is the model that the player respawns as
local oldModel = player.Character -- this is the player's character
local newModel = model:Clone() -- cloning the model
newModel.Name = player.Name -- changes the the models name to the player's name
player.Character = newModel --set's player's character to the model we cloned
newModel.Parent = game.Workspace --parent's to workspace
newModel:SetPrimaryPartCFrame(oldModel.HumanoidRootPart.CFrame) -- set's the CFrame to the respawing pad
for _, object in ipairs(game.StarterPlayer.StarterPlayerScripts:GetChildren()) do -- this just copies all the scripts from startercharacterscripts, you can do this with player scripts too. Just replace StarterCharacterScripts with StarterPlayerScripts
print(object)
local newObject = object:Clone() -- clones the script
newObject.Parent = newModel -- parents it
end
oldModel:Destroy() -- destroys the old player character
end)
I tried this out on the original Roblox avatar, as in the one owned by Roblox themselves and it worked just fine, but when using it on any other one the player couldnt move and threw an error:
Workspace.Part.Script:13: attempt to index nil with ‘Parent’
Obviously this means that the clone function returned nil, but this made no sense because the print statement printed the playermodule in starterplayerscripts, so it had found an instance to clone, so why was it returning nil when cloning? I also don’t know why this doesn’t work with any other model other than the original roblox one I tested, including other avatars I imported into my game that are all r15, same as the Roblox official one. Completely confused here.
I did that and although it at first didn’t make a difference, it was likely because the models were anchored in the humanoidrootpart, my bad. It’s annoying because I knew that would be an issue, I just didn’t think of it at the time. I am still getting the same error though, telling me that the clone returrns nil. Here is the current code:
script.Parent.ProximityPrompt.Triggered:Connect(function(player)
-- arguments for the player that is respawing and the model the player morphs into.
local model = game:GetService("ReplicatedStorage").IShowSpeed:Clone() --This is the model that the player respawns as
local oldModel = player.Character -- this is the player's character
local newModel = model:Clone() -- cloning the model
newModel.Name = player.Name -- changes the the models name to the player's name
player.Character = newModel --set's player's character to the model we cloned
newModel.Parent = game.Workspace --parent's to workspace
newModel:SetPrimaryPartCFrame(oldModel.HumanoidRootPart.CFrame) -- set's the CFrame to the respawing pad
oldModel:FindFirstChild("Animate"):Clone().Parent = newModel
for _, object in ipairs(game.StarterPlayer.StarterPlayerScripts:GetChildren()) do -- this just copies all the scripts from startercharacterscripts, you can do this with player scripts too. Just replace StarterCharacterScripts with StarterPlayerScripts
print(object)
local newObject = object:Clone() -- clones the script
newObject.Parent = newModel -- parents it
end
oldModel:Destroy() -- destroys the old player character
end)
For all intents and purposes, this issue is fixed as the player can morph into the given rig with no issues but I’d like to know the solution to this error if possible. I ran the script without the for loop to copy over the player scripts and it seems to work fine, which kinda confuses me. How is the player able to even control this new model without the player control scripts? Are they still active from starterplayerscripts after the player’s character is destroyed?