Help with cloning the player's character

Hello,
So I’m trying to making the player’s dead character stay, but they will respawn, so basically have a clone on it. When I clone it in Server Mode in studio, I can do it perfectly, but when I do it through a script, it doesn’t work:

		-- clone the player's character
		
		local duplicated = workspace[character.Name]:Clone()
		print('cloned')
		-- make sure the character exists and the player didn't leave before this ran
		if not duplicated then
			print('doesn\'t exist')
			return
		end
		-- change the name to prevent other scripts having issues.
		duplicated.Name = 'DeadCharacter'
		-- parent the player's character to the dead character's folder
		duplicated.Parent = workspace.DeadCharacters
		-- delete the scripts
		duplicated.Animate:Destroy()
		duplicated.Health:Destroy()
		-- loop through the character's descendants
		for _, part in next,duplicated:GetDescendants() do
			if part:IsA('BasePart') then
				-- anchor the parts and make them collidable
				part.Anchored = true
				part.CanCollide = true
			end
		end
		duplicated:SetPrimaryPartCFrame(character:GetPrimaryPartCFrame())
		print('complete!')

“Doesn’t Exist” prints, but why? I’m cloning the player’s character, so it should still work, how else would I achieve this? I mean I cannot think of another method of cloning the player’s character, but I’m 99.999999% that this is possible.

1 Like

So an object doesn’t exist if it isn’t in the garbage collection or its state is nil. When you clone something its parent is nil. You need to parent it to workspace and then check if it exists. Alternativly you can just move the check after you parent and delete the scripts.

I changed my script to this:

		if not game.ServerStorage.IsRunning.Value then
			-- make sure the game is actually running
			return
		end

		-- clone the player's character
		
		local duplicated = workspace[character.Name]:Clone()
		print('cloned')
		-- make sure the character exists and the player didn't leave before this ran

		-- change the name to prevent other scripts having issues.
		duplicated.Name = 'DeadCharacter'
		-- parent the player's character to the dead character's folder
		duplicated.Parent = workspace.DeadCharacters
		-- delete the scripts
		duplicated.Animate:Destroy()
		duplicated.Health:Destroy()
		-- loop through the character's descendants
		for _, part in next,duplicated:GetDescendants() do
			if part:IsA('BasePart') then
				-- anchor the parts and make them collidable
				part.Anchored = true
				part.CanCollide = true
			end
		end
		duplicated:SetPrimaryPartCFrame(character:GetPrimaryPartCFrame())
		print('complete!')

And I get the error:

I was messing around with your script and it seems that you cannot clone the player’s character? I do not know. But, I was able to get it to work if you make a function that clones the character in a different way. Use this function to clone the character instead of the traditional way and it should work.

local function clone(model)
	local newModel = Instance.new("Model")
	newModel.Name = model.Name
				
	for i, instance in pairs(model:GetChildren()) do
		local newInstance = instance:Clone()
		newInstance.Parent = newModel
	end
	return newModel
end

Hope this helps! :smiley:

1 Like

Player characters aren’t archivable by default. This is a property of the character model which you need to set to true before cloning, or else it can’t be accessed.

5 Likes