My animation will start, but won’t stop. Here is my code:
function ChangeActive()
if Active == false then
local RadioAnimation = script.RadioAnimation
local player = game.Players.LocalPlayer
local char = player.Character
local hum = char.Humanoid
local RadioAnimationTrack = hum:LoadAnimation(RadioAnimation)
Active = true
wait(0.1)
RadioAnimationTrack:Play()
ActiveChange.Text = "(T) Active"
ActiveChange.Parent.ImageColor3 = Color3.fromRGB(255,0,0)
elseif Active == true then
local RadioAnimation = script.RadioAnimation
local player = game.Players.LocalPlayer
local char = player.Character
local hum = char.Humanoid
local RadioAnimationTrack = hum:LoadAnimation(RadioAnimation)
Active = false
wait(0.1)
RadioAnimationTrack:Stop()
ActiveChange.Text = "(T) Inactive"
ActiveChange.Parent.ImageColor3 = Color3.fromRGB(11,101,107)
end
end
You are basically loading in the same animation again, and assign that loaded track to the RadioAnimationTrack variable. So basically you aren’t trying to stop the running track.
Instead of reassigning it, just run :Stop() on the already running track.
Also, you will need to have a variable outside the function, so you can actually reference it when you need to Stop it.
You also should assign common variables at a place where all functions can access it, you can check the below script:
local RadioAnimationTrack
local RadioAnimation = script.RadioAnimation
local player = game.Players.LocalPlayer
local char = player.Character
local hum = char.Humanoid
function ChangeActive()
if Active == false then
RadioAnimationTrack = hum:LoadAnimation(RadioAnimation)
Active = true
wait(0.1)
RadioAnimationTrack:Play()
ActiveChange.Text = "(T) Active"
ActiveChange.Parent.ImageColor3 = Color3.fromRGB(255,0,0)
elseif Active == true then
Active = false
wait(0.1)
RadioAnimationTrack:Stop()
ActiveChange.Text = "(T) Inactive"
ActiveChange.Parent.ImageColor3 = Color3.fromRGB(11,101,107)
end
end