Tower Preview won't play its animation

I am making a Tower Defense game called Tower Defense: Redefined I am slowly making progress, but this is one of the first problems I have encountered.

External Media

The Tower Preview won’t play its animation for some reason.

Here is some of my code:

				local ViewportCamera = Instance.new("Camera");
				ViewportCamera.CameraType = Enum.CameraType.Scriptable;
				ViewportCamera.Parent = TowerPreview;
				
				local ViewportPoint = Vector3.new(0, 0, 0);
				TowerPreview.CurrentCamera = ViewportCamera;
				
				Tower:PivotTo(CFrame.new(ViewportPoint));
				Tower.Parent = TowerPreview;
				
				local Humanoid = Tower:WaitForChild("Humanoid");
				local Animator = Humanoid:WaitForChild("Animator");
				local Idle = Tower:WaitForChild("Idle");
				Animator:LoadAnimation(Idle):Play();
				
				RunService.RenderStepped:Connect(function()
					local Cframe, Size = Tower:GetBoundingBox();
					local Max = math.max(Size.X, Size.Y, Size.Z);
					local Distance = (Max/math.tan(math.rad(ViewportCamera.FieldOfView))) * 1;
					local CurrentDistance = (Max/2) + Distance;

					ViewportCamera.CFrame = CFrame.Angles(0, math.rad(Rotation), 0) * CFrame.new(ViewportPoint + Vector3.new(0, 0, CurrentDistance), ViewportPoint);

					Rotation += RotationSpeed;
				end);

Any help is appreciated.

Can you send your entire code?

local Players = game:GetService("Players");
local TweenService = game:GetService("TweenService");
local RunService = game:GetService("RunService");
local ReplicatedStorage = game:GetService("ReplicatedStorage");
local TowersContainer = ReplicatedStorage:WaitForChild("Towers");

local LocalPlayer = Players.LocalPlayer;

local PlayerGui = LocalPlayer:WaitForChild("PlayerGui");
local Shop = PlayerGui:WaitForChild("Shop");
local ShopFrame = Shop:WaitForChild("ShopFrame");
local Towers = ShopFrame:WaitForChild("Towers");
local PlayerUI = PlayerGui:WaitForChild("PlayerUI");
local UserInterface = PlayerUI:WaitForChild("UserInterface");

local Rotation = 0;
local RotationSpeed = 0.1;

task.spawn(function()
	for _, Button in pairs(UserInterface:GetChildren()) do
		if Button:IsA("ImageButton") then
			Button.MouseEnter:Connect(function()
				TweenService:Create(Button, TweenInfo.new(1, Enum.EasingStyle.Exponential, Enum.EasingDirection.InOut), {Rotation = -15}):Play();
			end);

			Button.MouseLeave:Connect(function()
				TweenService:Create(Button, TweenInfo.new(1, Enum.EasingStyle.Exponential, Enum.EasingDirection.InOut), {Rotation = 0}):Play();
			end);
		end;
	end;
	
	UserInterface.Shop.MouseButton1Click:Connect(function()
		Shop.Enabled = not Shop.Enabled;
	end);
	
	for _, TowerPreview in pairs(Towers:GetChildren()) do
		if TowerPreview:IsA("ViewportFrame") then
			local Tower = TowersContainer:FindFirstChild(TowerPreview.Name);
			
			if Tower then
				local ViewportCamera = Instance.new("Camera");
				ViewportCamera.CameraType = Enum.CameraType.Scriptable;
				ViewportCamera.Parent = TowerPreview;
				
				local ViewportPoint = Vector3.new(0, 0, 0);
				TowerPreview.CurrentCamera = ViewportCamera;
				
				Tower:PivotTo(CFrame.new(ViewportPoint));
				Tower.Parent = TowerPreview;
				
				local Humanoid = Tower:WaitForChild("Humanoid");
				local Animator = Humanoid:WaitForChild("Animator");
				local Idle = Tower:WaitForChild("Idle");
				Animator:LoadAnimation(Idle):Play();
				
				RunService.RenderStepped:Connect(function()
					local Cframe, Size = Tower:GetBoundingBox();
					local Max = math.max(Size.X, Size.Y, Size.Z);
					local Distance = (Max/math.tan(math.rad(ViewportCamera.FieldOfView))) * 1;
					local CurrentDistance = (Max/2) + Distance;

					ViewportCamera.CFrame = CFrame.Angles(0, math.rad(Rotation), 0) * CFrame.new(ViewportPoint + Vector3.new(0, 0, CurrentDistance), ViewportPoint);

					Rotation += RotationSpeed;
				end);
			end;
		end;
	end;
end);

I got it to work! Apparently ViewportFrames cannot display animations but when you insert a WorldModel in and put your Model in it, it finally displays the animations.

If you are in a similar issue/the same issue like me then here’s part of my code just for reference:

			local WorldModel = TowerPreview:WaitForChild("WorldModel");
			local Tower = TowersContainer:WaitForChild(TowerPreview.Name);
			
			local ViewportCamera = Instance.new("Camera");
			ViewportCamera.CameraType = Enum.CameraType.Scriptable;
			ViewportCamera.Parent = TowerPreview;

			local ViewportPoint = Vector3.new(0, 0, 0);
			TowerPreview.CurrentCamera = ViewportCamera;

			Tower:PivotTo(CFrame.new(ViewportPoint));
			Tower.Parent = WorldModel;

			local Humanoid = Tower:WaitForChild("Humanoid");
			local Animator = Humanoid:WaitForChild("Animator");
			local Idle = Tower:WaitForChild("Idle");
			local IdleAnimation = Animator:LoadAnimation(Idle);
			IdleAnimation:Play();

			RunService.RenderStepped:Connect(function()
				local Cframe, Size = Tower:GetBoundingBox();
				local Max = math.max(Size.X, Size.Y, Size.Z);
				local Distance = (Max/math.tan(math.rad(ViewportCamera.FieldOfView))) * 1;
				local CurrentDistance = (Max/2) + Distance;

				ViewportCamera.CFrame = CFrame.Angles(0, math.rad(Rotation), 0) * CFrame.new(ViewportPoint + Vector3.new(0, 0, CurrentDistance), ViewportPoint);

				Rotation += RotationSpeed;
			end);

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.