I know that I probably shouldn’t report it in bug reports, but it feels like bug because it works in workspace and other place
Animations work in workspace (With basically same way of getting and playing animation)
Animations for models in viewport appear in “PlayingAnimationTracks”
It works in other place (with different script but that uses basically same way of getting and playing animation)
I’ve asked help from other people, none helped
Final script with help from other peopl
function module.LoadViewport(Template: Frame, ToiletName)
local Model = game:GetService("ReplicatedStorage").Assets.Toilets:FindFirstChild(ToiletName)
if not Model then return end
local ViewportFrame: ViewportFrame = Template:WaitForChild("ViewportFrame")
local WorldModel: WorldModel = ViewportFrame:WaitForChild("WorldModel")
local NewModel: Model = Model:Clone()
for i,v in pairs(NewModel:GetDescendants()) do
if v:IsA("BasePart") then
v.Anchored = false
end
end
--NewModel.HumanoidRootPart.Anchored = true
--NewModel.Parent = WorldModel
local Position = NewModel:GetPivot() * CFrame.new(-5, -1.5, -5)
local LookAt = CFrame.lookAt(Position.Position, NewModel.PrimaryPart.Position)
local Camera = Instance.new("Camera")
Camera.CFrame = LookAt
Camera.Parent = ViewportFrame
ViewportFrame.CurrentCamera = Camera
task.spawn(function()
Model.Parent = workspace
Model.Parent = WorldModel
local hum = NewModel:FindFirstChildOfClass("Humanoid")
local anim = if game:GetService("ReplicatedStorage").Assets.Anims:FindFirstChild(ToiletName) then game:GetService("ReplicatedStorage").Assets.Anims:FindFirstChild(ToiletName).Idle else game:GetService("ReplicatedStorage").Assets.Anims.Toilet.Idle
local CloneAnim = Instance.new("Animation")
CloneAnim.AnimationId = "rbxassetid://74789765315759"
CloneAnim.Parent = NewModel
print(CloneAnim.Name, CloneAnim.AnimationId)
local Animator = hum:FindFirstChildOfClass("Animator") or Instance.new("Animator", hum)
local Animation = Animator:LoadAnimation(CloneAnim)
Animation.Looped = true
Animation:Play()
local animTracks = Animator:GetPlayingAnimationTracks()
local animationTrack: AnimationTrack = animTracks[1]
animationTrack:Stop()
task.wait(0.1)
animationTrack:Play()
--for i=1, 10 do
-- Animation:Play()
-- print("Trying to play animation:", ToiletName)
-- task.wait(2)
--end
end)
end
Script before help from other people
function module.LoadViewport(Template: Frame, ToiletName)
local Model = game:GetService("ReplicatedStorage").Assets.Toilets:FindFirstChild(ToiletName)
if not Model then return end
local ViewportFrame: ViewportFrame = Template:WaitForChild("ViewportFrame")
local WorldModel: WorldModel = ViewportFrame:WaitForChild("WorldModel")
local NewModel: Model = Model:Clone()
for i,v in pairs(NewModel:GetDescendants()) do
if v:IsA("BasePart") then
v.Anchored = false
end
end
NewModel.Parent = WorldModel
local hum = NewModel:FindFirstChildOfClass("Humanoid")
local anim = if game:GetService("ReplicatedStorage").Assets.Anims:FindFirstChild(ToiletName) then game:GetService("ReplicatedStorage").Assets.Anims:FindFirstChild(ToiletName).Idle else game:GetService("ReplicatedStorage").Assets.Anims.Toilet.Idle
local Animator = hum:FindFirstChildOfClass("Animator") or Instance.new("Animator", hum)
local Animation = Animator:LoadAnimation(anim)
Animation.Looped = true
Animation:Play()
--print(Animator:GetPlayingAnimationTracks())
local Position = NewModel:GetPivot() * CFrame.new(-5, -1.5, -5)
local LookAt = CFrame.lookAt(Position.Position, NewModel.PrimaryPart.Position)
local Camera = Instance.new("Camera")
Camera.CFrame = LookAt
Camera.Parent = ViewportFrame
ViewportFrame.CurrentCamera = Camera
end
Script in other place
local module = {}
function module.GetAnimationTrack(Model: Model, animName: string)
local Humanoid = Model:FindFirstChildOfClass("Humanoid")
local AnimationControl
if Humanoid then
AnimationControl = Humanoid:FindFirstChildOfClass("Animator") or Instance.new("Animator", Humanoid)
else
AnimationControl = Model:FindFirstChildOfClass("AnimationController") or Instance.new("AnimationController", Model)
end
local AnimationsFolder = Model:FindFirstChild("Animations")
local AnimationInstance = (AnimationsFolder and AnimationsFolder:FindFirstChild(animName))
if not AnimationInstance then
warn("Couldn't find animation:", animName)
return
end
if not AnimationInstance:IsA("Animation") then
warn("Found instance class name is not Animation:", AnimationInstance.Name)
return
end
local PlayingAnimTracks = AnimationControl:GetPlayingAnimationTracks()
local requiredAnim
for i,v: AnimationTrack in pairs(PlayingAnimTracks) do
if v.Name == animName then
requiredAnim = v
break
end
end
if not requiredAnim then
requiredAnim = AnimationControl:LoadAnimation(AnimationInstance)
end
return requiredAnim
end
function module.SetupViewportAnimation(frame: Frame, Model: Model, parent)
if not frame.Parent or not frame.Parent.Parent or not frame.Parent.Parent.Parent then -- (Just not frame.Parent would be enough though, I think so at least)
if not parent then
warn("Couldn't set animation, because frame is not parented and parent is not given")
return
end
frame.Parent = parent
end
local ViewportFrame = frame:FindFirstChildOfClass("ViewportFrame")
if not ViewportFrame then return end
local WorldModel = ViewportFrame:FindFirstChildOfClass("WorldModel")
if not WorldModel then return end
Model = Model or WorldModel:FindFirstChildOfClass("Model")
if not Model then
warn("Couldn't set animation, because model not found")
return
end
local AnimName = Model:GetAttribute("ViewportAnim") or shared.Settings.Viewports.Default_Idle_Animation_Name
local AnimationTrack: AnimationTrack = module.GetAnimationTrack(Model, AnimName)
if not AnimationTrack then
warn("Couldn't find animation track:", AnimName)
return
end
AnimationTrack:Play()
end
return module
Expected behavior
Animation should play in viewport/WorldModel, but it does not