Clone Command | Seat & Animation Bug

  1. What do you want to achieve?

A simple /clone command, that allows the player to clone themselves and what ever their doing. The clone should be doing any dance that they are doing, and should be at the same spot they are at.

  1. What is the issue?

If a player does /clone while dancing, it does not play the custom animations onto the new clone. It only plays default Roblox emotes. If a player is sitting down and does /clone while sitting. It clones the player, but moves the clone up a few studs.

https://gyazo.com/3526840e141b6ed4cc112d8e001a538a
https://gyazo.com/588fd859c609e364bbff5e052dd88dd5

  1. What solutions have you tried so far?

I have not found a resolution to fixing both of these bugs. My code is already using the way to sync a player normally and grabbing the animations. Im curious why its not duplicating the CUSTOM EMOTES onto the clone?
Im guessing I can also create another if statement to see if the player that did /clone is sitting, and then if they are sitting I am able to keep them at the same studd/Y height?

Here is the full code →

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local container = workspace
local clone

local createClone = function(character)
	local humanoid = character:FindFirstChild("Humanoid")
	local description = humanoid:GetAppliedDescription()
	if humanoid then
		--Setup clone
		character.Archivable = true
		clone = character:Clone()
		local cloneHumanoid = clone.Humanoid
		clone.Name = "Clone"
		local specialChar = false
		if clone:FindFirstChild("Chest") then
			specialChar = true
		end
		for a,b in pairs(clone:GetDescendants()) do
			if b:IsA("Humanoid") then
				b.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
			elseif b:IsA("BillboardGui") then
				b:Destroy()
			elseif b:IsA("Weld") and b.Part1 ~= nil then
				b.Part0 = b.Parent
				if clone:FindFirstChild(b.Part1.Name) then
					b.Part1 = clone[b.Part1.Name]
				elseif not specialChar then
					b:Destroy()
				end
			end
		end
		
		if not container:FindFirstChild("Clones") then
			local Folder = Instance.new("Folder")
			Folder.Name = "Clones"
			Folder.Parent = container
			clone.Parent = Folder
		else
			clone.Parent = container:FindFirstChild("Clones")
		end
		
		cloneHumanoid:ApplyDescription(description)
		
		local animator = humanoid:FindFirstChild("Animator")
		local animator2 = cloneHumanoid:FindFirstChild("Animator")
		
		if animator and animator2 then
			
			for _, v in pairs(animator:GetPlayingAnimationTracks()) do
				local track = animator2:LoadAnimation(v.Animation)
				track.Priority = Enum.AnimationPriority.Action
				track:Play()
				track.TimePosition = v.TimePosition
				track:AdjustSpeed(v.Speed)
			end
			
		end
		
		
	end
end

local clone = function(plr)
	local hrp = character:FindFirstChild("HumanoidRootPart")
	if hrp then
		local clone = createClone(character)
		--local humanoid = clone:FindFirstChild("Humanoid")
		--humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.Subject
		--humanoid.HealthDisplayDistance = 25
		--humanoid.NameDisplayDistance = 25
		--humanoid.DisplayName = " "
		--local cloneHrp = clone:FindFirstChild("HumanoidRootPart")
		--cloneHrp.CFrame = hrp.CFrame * CFrame.new(0, 5, 0)
		--clone.Name = " "
	end
end

game.ReplicatedStorage.EVENTS.CloneEvent.OnClientEvent:Connect(function(rigType)
	clone(rigType)
end)


local Humanoid = character:WaitForChild("Humanoid")

Humanoid.Died:Connect(function()
	
	for _,v in ipairs(container:GetChildren()) do
		if v.Name == "Clones" then
			v:Destroy()
		end
	end
	
end)
local players = game:GetService("Players")
local physics = game:GetService("PhysicsService")
physics:CreateCollisionGroup("PlayerCollisions")
physics:CollisionGroupSetCollidable("PlayerCollisions", "PlayerCollisions", false)

local commands = {";clone", ":clone", "/clone"}

local function loopCharacterRecursive(object)
	for _, child in ipairs(object:GetChildren()) do
		if child:IsA("BasePart") then
			child.CustomPhysicalProperties = PhysicalProperties.new(math.huge, math.huge, math.huge, math.huge, math.huge)
		elseif child:IsA("Accessory") then
			loopCharacterRecursive(child)
		elseif child:IsA("Tool") then
			loopCharacterRecursive(child)
		end
	end
end

local function setCollisionGroupRecursive(object)
	for _, child in ipairs(object:GetChildren()) do
		if child:IsA("BasePart") then
			physics:SetPartCollisionGroup(child, "PlayerCollisions")
		elseif child:IsA("Accessory") then
			setCollisionGroupRecursive(child)
		elseif child:IsA("Tool") then
			setCollisionGroupRecursive(child)
		end
	end
end

local function removeCollisionGroupRecursive(object)
	for _, child in ipairs(object:GetChildren()) do
		if child:IsA("BasePart") then
			physics:SetPartCollisionGroup(child, "Default")
		elseif child:IsA("Accessory") then
			removeCollisionGroupRecursive(child)
		elseif child:IsA("Tool") then
			removeCollisionGroupRecursive(child)
		end
	end
end

local function onPlayerAdded(player)
	local function onPlayerChatted(message)
		for _, command in ipairs(commands) do
			if message:lower():match("^"..command) then
				if player.Clones.Value then
					removeCollisionGroupRecursive(player.Clones.Value)
					player.Clones.Value:Destroy()
					player.Clones.Value = nil
				end

				local character = player.Character or player.CharacterAdded:Wait()
				local humanoid = character:WaitForChild("Humanoid")
				local animator = humanoid:WaitForChild("Animator")
				character.Archivable = true

				local characterClone = character:Clone()
				player.Clones.Value = characterClone
				characterClone.Name = character.Name.."'s Clone"
				characterClone:PivotTo(character.PrimaryPart.CFrame)
				setCollisionGroupRecursive(characterClone)
				loopCharacterRecursive(characterClone)
				local hrpClone = characterClone:WaitForChild("HumanoidRootPart")
				if humanoid.SeatPart then
					local weld = Instance.new("Weld")
					weld.Name = "SeatWeldClone"
					weld.Part0 = humanoid.SeatPart
					weld.Part1 = hrpClone
					weld.C0 = hrpClone.CFrame
					weld.C1 = humanoid.SeatPart.CFrame
					weld.Parent = humanoid.SeatPart
				end
				characterClone.Parent = workspace

				local function onHumanoidCloneDied()
					removeCollisionGroupRecursive(characterClone)
					task.delay(3, function()
						characterClone:Destroy()
						player.Clones.Value = nil
					end)
				end
				
				local function onCharacterCloneChildAdded(child)
					if child:IsA("Tool") then
						setCollisionGroupRecursive(child)
					elseif child:IsA("Accessory") then
						setCollisionGroupRecursive(child)
					elseif child:IsA("BasePart") then
						setCollisionGroupRecursive(child)
					end
				end
				
				local function onCharacterCloneChildRemoved(child)
					if child:IsA("Tool") then
						removeCollisionGroupRecursive(child)
					elseif child:IsA("Accessory") then
						removeCollisionGroupRecursive(child)
					elseif child:IsA("BasePart") then
						removeCollisionGroupRecursive(child)
					end
				end
				
				characterClone.ChildAdded:Connect(onCharacterCloneChildAdded)
				characterClone.ChildRemoved:Connect(onCharacterCloneChildRemoved)
				
				local humanoidClone = characterClone:WaitForChild("Humanoid")
				humanoidClone.Died:Connect(onHumanoidCloneDied)

				local animatorClone = humanoidClone:WaitForChild("Animator")

				for _, track in ipairs(animator:GetPlayingAnimationTracks()) do
					local trackClone = animatorClone:LoadAnimation(track.Animation)
					repeat task.wait() until trackClone.Length > 0
					trackClone.Priority = track.Priority
					trackClone:AdjustSpeed(track.Speed)
					trackClone.TimePosition = track.TimePosition
					trackClone:Play()
				end
			end
		end
	end

	local function onCharacterAdded(character)
		local function onHumanoidDied()
			if player.Clones.Value then
				removeCollisionGroupRecursive(player.Clones.Value)
				player.Clones.Value:Destroy()
				player.Clones.Value = nil
			end
		end

		local function onCharacterChildAdded(child)
			if child:IsA("Tool") then
				local function onToolEquipped()
					if player.Clones.Value then
						local toolClone = child:Clone()
						toolClone.Parent = player.Clones.Value
						player.Clones.Value.Humanoid:UnequipTools()
						player.Clones.Value.Humanoid:EquipTool(toolClone)
					end
				end
				
				child.Equipped:Connect(onToolEquipped)
				setCollisionGroupRecursive(child)
			elseif child:IsA("Accessory") then
				local function onAccessoryAdded()
					if player.Clones.Value then
						local accessoryClone = child:Clone()
						player.Clones.Value.Humanoid:AddAccessory(accessoryClone)
						accessoryClone.Parent = player.Clones.Value
					end
				end
				
				onAccessoryAdded()
				setCollisionGroupRecursive(child)
			elseif child:IsA("BasePart") then
				setCollisionGroupRecursive(child)
			end
		end

		local function onCharacterChildRemoved(child)
			if child:IsA("Tool") then
				local function onToolUnequipped()
					if player.Clones.Value then
						player.Clones.Value.Humanoid:UnequipTools()
					end
				end
				
				child.Unequipped:Connect(onToolUnequipped)
				setCollisionGroupRecursive(child)
			elseif child:IsA("Accessory") then
				local function onAccessoryRemoved()
					if player.Clones.Value then
						player.Clones.Value:FindFirstChild(child.Name):Destroy()
					end
				end
				
				onAccessoryRemoved()
				setCollisionGroupRecursive(child)
			elseif child:IsA("BasePart") then
				setCollisionGroupRecursive(child)
			end
		end

		character.ChildAdded:Connect(onCharacterChildAdded)
		character.ChildRemoved:Connect(onCharacterChildRemoved)

		local humanoid = character:WaitForChild("Humanoid")
		humanoid.Died:Connect(onHumanoidDied)
	end

	local function onCharacterAppearanceLoaded(character)
		setCollisionGroupRecursive(character)
	end

	local function onCharacterRemoving(character)
		removeCollisionGroupRecursive(character)
	end

	player.Chatted:Connect(onPlayerChatted)
	player.CharacterAdded:Connect(onCharacterAdded)
	player.CharacterAppearanceLoaded:Connect(onCharacterAppearanceLoaded)
	player.CharacterRemoving:Connect(onCharacterRemoving)

	local clone = Instance.new("ObjectValue")
	clone.Name = "Clones"
	clone.Parent = player
end

players.PlayerAdded:Connect(onPlayerAdded)

This script is working well on my end, I tested with custom emotes & seats too.

So my script is a local sided script in StarterGui, and gets called by an event. I didn’t want players seeing the clones, because it could cause more Lag for other players. I’ll have to implement this into my local script and see how it goes.
Thankyou.
This should work server sided, ill have to switch.

So I was messing around with the code, for my local sided and figured that this worked way better →

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local container = workspace
local clone

local createClone = function(character)
	local humanoid = character:FindFirstChild("Humanoid")
	local description = humanoid:GetAppliedDescription()
	if humanoid then
		--Setup clone
		character.Archivable = true
		clone = character:Clone()
		local cloneHumanoid = clone.Humanoid
		clone.Name = "Clone"
		local specialChar = false
		if clone:FindFirstChild("Chest") then
			specialChar = true
		end
		for a,b in pairs(clone:GetDescendants()) do
			if b:IsA("Humanoid") then
				b.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
			elseif b:IsA("BillboardGui") then
				b:Destroy()
			elseif b:IsA("Weld") and b.Part1 ~= nil then
				b.Part0 = b.Parent
				if clone:FindFirstChild(b.Part1.Name) then
					b.Part1 = clone[b.Part1.Name]
				elseif not specialChar then
					b:Destroy()
				end
			end
		end
		
		local hrpClone = clone:FindFirstChild("HumanoidRootPart")

		if humanoid.SeatPart then
			local weld = Instance.new("Weld")
			weld.Name = "SeatWeldClone"
			weld.Part0 = humanoid.SeatPart
			weld.Part1 = hrpClone
			weld.C0 = hrpClone.CFrame
			weld.C1 = humanoid.SeatPart.CFrame
			weld.Parent = humanoid.SeatPart
		end
		
		if not container:FindFirstChild("Clones") then
			local Folder = Instance.new("Folder")
			Folder.Name = "Clones"
			Folder.Parent = container
			clone.Parent = Folder
		else
			clone.Parent = container:FindFirstChild("Clones")
		end
		
		cloneHumanoid:ApplyDescription(description)
		
		local animator = humanoid:FindFirstChild("Animator")
		local animator2 = cloneHumanoid:FindFirstChild("Animator")
		
		if animator and animator2 then
			
			for _, v in pairs(animator:GetPlayingAnimationTracks()) do
				local track = animator2:LoadAnimation(v.Animation)
				repeat task.wait() until track.Length > 0
				track.Priority = v.Priority
				track:Play()
				track:AdjustSpeed(v.Speed)
				track.TimePosition = v.TimePosition
			end
			
		end

	end
end

local clone = function(plr)
	local hrp = character:FindFirstChild("HumanoidRootPart")
	if hrp then
		local clone = createClone(character)
	end
end

game.ReplicatedStorage.EVENTS.CloneEvent.OnClientEvent:Connect(function(rigType)
	clone(rigType)
end)


local Humanoid = character:WaitForChild("Humanoid")

Humanoid.Died:Connect(function()
	
	for _,v in ipairs(container:GetChildren()) do
		if v.Name == "Clones" then
			v:Destroy()
		end
	end
	
end)

Messed around with the Animations and got that to work.

Only problem is the CUSTOM seat animations. I need to figure a way to get them to play, because at the moment yours isn’t playing the CUSTOM seat animations either. Though it did fix the problem of the clone being put up studds.

It only plays DEFAULT roblox sit animations.

The way i’m playing an animation for a player →

local anim

for i, seat in pairs(script.Parent:GetDescendants()) do
	if seat:IsA("Seat") then
		local animID = seat.AnimID
		local animIDBlox = seat.AnimIDBlox
		seat.Changed:Connect(function(property)
			if property ~= "Occupant" then return end
			
			local occupant = seat.Occupant
			
			if occupant then
				local char = occupant.Parent
				local humanoid = char.Humanoid
				local plr = game.Players:GetPlayerFromCharacter(char)
				if plr and humanoid.RigType == Enum.HumanoidRigType.R15 then -- Normal Character
					anim = humanoid:LoadAnimation(animID)
					anim:Play()
				elseif plr and humanoid.RigType == Enum.HumanoidRigType.R6 then -- Blox Character
					anim = humanoid:LoadAnimation(animIDBlox)
					anim:Play()
				end
			elseif anim then
				anim:Stop()
			end
		end)
	end
end

How could I check if their is a seat animation being played when they clone?

Sorry to bump this topic.

Are you loading your custom animations through the humanoid or animator?

Ah shoot. Looks like i’m doing it through Humanoid. Let me try and fix that and see if that works!

anim = humanoid:LoadAnimation(animID)

Didn’t realize that! ^

Edit :

I just changed the anim script to >

local char = occupant.Parent
				local humanoid = char.Humanoid
				local animator = humanoid:FindFirstChild("Animator")
				local plr = game.Players:GetPlayerFromCharacter(char)
				if plr and humanoid.RigType == Enum.HumanoidRigType.R15 then -- Normal Character
					anim = animator:LoadAnimation(animID)
					anim:Play()
				elseif plr and humanoid.RigType == Enum.HumanoidRigType.R6 then -- Blox Character
					anim = animator:LoadAnimation(animIDBlox)
					anim:Play()
				end

Load onto the animator. Though it still didn’t fix the problem? Wonder why this is?

That’d just be playing the animation through the animator instance what I mean is the following.

for _, track in ipairs(humanoid:GetPlayingAnimationTracks()) do

You’re iterating over the animation tracks being played through the animator instance but not the humanoid instance.

Something like this?

for _, track in ipairs(humanoid:GetPlayingAnimationTracks()) do
	track = animator:LoadAnimation(animID)
	track:Play()
end

Little confused, by what you are trying to say?

How should I rewrite my seat script?

In your original script you had the following block of code.

for _, v in pairs(animator:GetPlayingAnimationTracks()) do
	local track = animator2:LoadAnimation(v.Animation)
	track.Priority = Enum.AnimationPriority.Action
	track:Play()
	track.TimePosition = v.TimePosition
	track:AdjustSpeed(v.Speed)
end

You should include this block of code too.

for _, v in pairs(humanoid:GetPlayingAnimationTracks()) do
	local track = animator2:LoadAnimation(v.Animation)
	track.Priority = Enum.AnimationPriority.Action
	track:Play()
	track.TimePosition = v.TimePosition
	track:AdjustSpeed(v.Speed)
end

“humanoid” would be a reference to the original player’s character’s humanoid instance.

local animator = humanoid:FindFirstChild("Animator")
		local animator2 = cloneHumanoid:FindFirstChild("Animator")
		
		if animator and animator2 then
			
			for _, v in pairs(animator:GetPlayingAnimationTracks()) do
				local track = animator2:LoadAnimation(v.Animation)
				repeat task.wait() until track.Length > 0
				track.Priority = v.Priority
				track:Play()
				track:AdjustSpeed(v.Speed)
				track.TimePosition = v.TimePosition
			end
			
			for _, v in pairs(humanoid:GetPlayingAnimationTracks()) do
				local track = animator2:LoadAnimation(v.Animation)
				track.Priority = Enum.AnimationPriority.Action
				track:Play()
				track.TimePosition = v.TimePosition
				track:AdjustSpeed(v.Speed)
			end
			
		end

Something like this? Or outside?
Oh I understand. So now it checks also humanoid and animator.

Though this still doesn’t seem to make a difference? I wonder why the clone on the seat isn’t moving. It works for default sit animation but not custom… hm.

You’d need to reproduce however you’re playing the animation for the original character to the cloned character.


		if humanoid.SeatPart then
			local weld = Instance.new("Weld")
			weld.Name = "SeatWeldClone"
			weld.Part0 = humanoid.SeatPart
			weld.Part1 = hrpClone
			weld.C0 = hrpClone.CFrame
			weld.C1 = humanoid.SeatPart.CFrame
			weld.Parent = humanoid.SeatPart
			
			if humanoid.Sit == true then
				for _,v in pairs (humanoid.SeatPart:GetChildren()) do
					if v:IsA("Animation") then
						if v.Name == "AnimID" then
							if humanoid.RigType == Enum.HumanoidRigType.R15 then
								print("r15")
							end
						elseif v.Name == "AnimIDBlox" then
							if humanoid.RigType == Enum.HumanoidRigType.R6 then
								print("r6")
							end
						end
					end
				end
			end
			
		end

I’ve come up with something like this…
image

Though I don’t think this is as efficient and is confusing me.
I want to try and load the animations that are in the seat onto the players animator.