Cloned Model won't animate on Local Script

Hello,

So in this code a model gets cloned and put in a Viewport Frame, then it should play an animation there. Tho it need prints “Loaded”. Is it because of the local script?

for i, defender in defenders:GetChildren() do
	local stat = defenderStats[defender.Name]

	local button = gui.Defenders.Template:Clone()
	button.Name = defender.Name
	button.DefenderName.Text = defender.Name
	button.Visible = true
	button.Price.Text = stat.Price
	button.LayoutOrder = -stat.Price

	-- Viewport Code

	local defenderDisplay = defenders[defender.Name]:Clone()

	for _, model in button.ViewportFrame:GetChildren() do
		if model:IsA("Model") then
			model:Destroy()
		end
	end

	defenderDisplay:SetPrimaryPartCFrame(CFrame.new(-0.044, 19.221, 18.236) * CFrame.Angles(0, math.rad(165), 0))
	
	if defenderDisplay:FindFirstChild("Humanoid") then
		
	pcall(function()
		
		local humanoid = defenderDisplay.Humanoid
		local Animator = humanoid:WaitForChild("Animator")
		local idleAnimId = "rbxassetid://" .. defenderStats[defender.Name].Animations.Idle
		local Animation = Instance.new("Animation")
		Animation.AnimationId = idleAnimId
		
		local function Setup()
			local AnimationTrack = Animator:LoadAnimation(Animation)
			print("Loaded")
			AnimationTrack.Looped = true
			print(1)
			AnimationTrack:Play()
			print(2)
		end

		Setup()
	end)
end
	
	
	defenderDisplay.Parent = button.ViewportFrame
	button.Parent = gui.Defenders

	button.Activated:Connect(function()
		local allowedToSpawn = functions.RequestDefender:InvokeServer(defender.Name)
		if allowedToSpawn then
			addPlaceholder(button.Name)
		end
	end)
end

I tried remote events, although the player argument … the model to animate was always nil

Tell me if this works.

Change it to

local idleAnimId = "rbxassetid://" .. defenderStats[defender.Name].Animations.Idle.AnimationId
1 Like

You need to put the cloned model inside a WorldModel (which you should put inside the ViewportFrame)

1 Like

Both of your guys suggestions didn’t seem to work. Here’s the overwritten Code if needed:

for i, defender in defenders:GetChildren() do
	local stat = defenderStats[defender.Name]

	local button = gui.Defenders.Template:Clone()
	button.Name = defender.Name
	button.DefenderName.Text = defender.Name
	button.Visible = true
	button.Price.Text = stat.Price
	button.LayoutOrder = -stat.Price

	-- Viewport Code

	local defenderDisplay = defenders[defender.Name]:Clone()

	for _, model in button.ViewportFrame.WorldModel:GetChildren() do
		if model:IsA("Model") then
			model:Destroy()
		end
	end

	defenderDisplay:SetPrimaryPartCFrame(CFrame.new(-0.044, 19.221, 18.236) * CFrame.Angles(0, math.rad(165), 0))

	if defenderDisplay:FindFirstChild("Humanoid") then

		pcall(function()

			local humanoid = defenderDisplay.Humanoid
			local Animator = humanoid:WaitForChild("Animator")
			local idleAnimId = "rbxassetid://" .. defenderStats[defender.Name].Animations.Idle
			local Animation = Instance.new("Animation")
			Animation.AnimationId = idleAnimId

			local function Setup()
				local AnimationTrack = Animator:LoadAnimation(Animation)
				print("Loaded")
				AnimationTrack.Looped = true
				print(1)
				AnimationTrack:Play()
				print(2)
			end

			Setup()
		end)
	end


	defenderDisplay.Parent = button.ViewportFrame.WorldModel
	button.Parent = gui.Defenders

	button.Activated:Connect(function()
		local allowedToSpawn = functions.RequestDefender:InvokeServer(defender.Name)
		if allowedToSpawn then
			addPlaceholder(button.Name)
		end
	end)
end

Also if you need to check something else, here’s the DefenderStatsModule:

defenderStats.Scouter = {
		Animations = {
			Idle = "86273756180377",
			Attack = "131188824794088"	
		},
		
		Stat = {
			Damage = 2,
			Cooldown = 1,
			Range = 6,
		},
		
		Price = 250,
		Placelimit = 5
	}

I think defenderDisplay needs to be parented first before you do anything with animation.

Screenshot 2024-11-23 143013
I’m now getting this Issue with this Code:

for i, defender in defenders:GetChildren() do
	local stat = defenderStats[defender.Name]
	local background, stroke = getDefenderRarity(stat.Rarity)

	local button = gui.Defenders.Template:Clone()
	button.Name = defender.Name
	button.DefenderName.Text = defender.Name
	button.Visible = true
	button.Price.Text = stat.Price
	button.LayoutOrder = -stat.Price
	button.BackgroundColor3 = background
	button.UIStroke.Color = stroke

	-- Viewport Code

	local defenderDisplay = defenders[defender.Name]:Clone()

	for _, model in button.ViewportFrame.WorldModel:GetChildren() do
		if model:IsA("Model") then
			model:Destroy()
		end
	end
	
	defenderDisplay.Parent = button.ViewportFrame.WorldModel
	button.ViewportFrame.WorldModel.PrimaryPart = defenderDisplay.HumanoidRootPart

	defenderDisplay:SetPrimaryPartCFrame(CFrame.new(-0.044, 19.221, 18.236) * CFrame.Angles(0, math.rad(165), 0))

	if defenderDisplay:FindFirstChild("Humanoid") then

		pcall(function()

			local humanoid = defenderDisplay.Humanoid
			local Animator = humanoid:WaitForChild("Animator")
			local idleAnimId = "rbxassetid://" .. defenderStats[defender.Name].Animations.Idle
			
			local Animation = Instance.new("Animation")
			Animation.AnimationId = idleAnimId
			Animation.Name = "Idle"
			Animation.Parent = defenderDisplay

			local function Setup()
				print(Animator, Animation)
				local AnimationTrack = Animator:LoadAnimation(Animation)
				print("Loaded")
				AnimationTrack.Looped = true
				print(1)
				AnimationTrack:Play()
				print(2)
			end

			Setup()
		end)
	end
	
	button.Parent = gui.Defenders

	button.Activated:Connect(function()
		local allowedToSpawn = functions.RequestDefender:InvokeServer(defender.Name)
		if allowedToSpawn then
			addPlaceholder(button.Name)
		end
	end)
end

Hmm, I don’t know what do about it. I’ve checked one of my older games which used had animated characters inside the ViewportFrame, I did it exactly the same

Edit: I noticed you set the PrimaryPart of the WorldModel to the character, maybe that’s why it’s causing errors.

After removing it entirely, i got this error:
image_2024-11-23_145352816

I ended up doing this. No errors appear anymore, however they still aren’t animated. I’ve checked if they are anchored and they are not.

I also noticed how it isnt me recommending this “LoadAnimation”. However it is, on the Server:

The Animator, also get’s recommened as: error-type