I’m trying to make an viewport frame to show the animation. But when i change the value when playtesting. The previous animation won’t stop. And the new one won’t play after the new animation.
My code so far
local emotePreviewCharacter = script.Parent
local humanoid = emotePreviewCharacter.Humanoid
local animationID = script.Emoted
local currentTrack = humanoid.Animator:LoadAnimation(script.Emoted)
currentTrack:Play()
animationID:GetPropertyChangedSignal("AnimationId"):Connect(function()
humanoid:Destroy()
local newHumanoid = Instance.new("Humanoid")
newHumanoid.Parent = script.Parent
humanoid = newHumanoid
local newAnimator = Instance.new("Animator")
newAnimator.Parent = humanoid
wait(0.1)
local newTrack = humanoid.Animator:LoadAnimation(script.Emoted)
newTrack:Play()
end)
you’re destroying the existing Humanoid and creating a new one, this is an unusual approach and leads to unexpected behavior, such as animations not stopping or starting correctly…
Instead of destroying the Humanoid you should just stop the current playing animation track and then load and play the new animation. I made an example
local emotePreviewCharacter = script.Parent
local humanoid = emotePreviewCharacter:WaitForChild("Humanoid")
local animationID = script:WaitForChild("Emoted")
local currentTrack = humanoid:LoadAnimation(animationID)
currentTrack:Play()
local function playNewAnimation()
if currentTrack then
currentTrack:Stop()
currentTrack = nil
end
currentTrack = humanoid:LoadAnimation(animationID)
currentTrack:Play()
end
animationID:GetPropertyChangedSignal("AnimationId"):Connect(playNewAnimation)