Hi! So, I am making some animations for viewmodels and when I use the Stop() function, the animation doesn’t stop for some reason. How can I fix this?
local hum = Players.LocalPlayer.Character:WaitForChild("Humanoid")
local ViewmodelHum = Viewmodel.Humanoid
local Viewmodel_Run = ViewmodelHum.Viewmodel_Run
local RunS = game:GetService("RunService")
function AnimationHandlerFunc()
local VMRunTrack = ViewmodelHum:LoadAnimation(Viewmodel_Run)
if hum.MoveDirection.Magnitude > 0 and VMRunTrack.IsPlaying == false then
VMRunTrack:Play()
elseif hum.MoveDirection.Magnitude == 0 and VMRunTrack.IsPlaying == true then
VMRunTrack:Stop()
end
end
RunS.Heartbeat:Connect(AnimationHandlerFunc)
The Stop method has a default fade time of 0.1 seconds, which means your animation will continue to affect the viewmodels for a short amount of time. If that’s your issue, try:
VMRunTrack:Stop(0.0001) -- or some other very small number
-- Alternatively (I don't know if this will work)
VMRunTrack:Stop(0)
You are creating a new Animation within the Animator of the Character each time your fire the function (which is every frame), so create it once outside of the function, then manipulate it when using the function.
local VMRunTrack = ViewmodelHum:LoadAnimation(Viewmodel_Run) -- create here
function AnimationHandlerFunc()
-- modify here
end
RunS.Heartbeat:Connect(AnimationHandlerFunc) -- Fire here
If you have to, use a table, or modules to properly store each animation that goes through so you can easily modify it