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
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:
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.