Help with animation script

how would i make this so when the player stops running the idle animation plays and when they start walking again the animation plays again ONLY IF TOGGLED = FALSE and if toggled = true then do nothing because it’s untoggled

local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character
local toggled = false
local Anim = Instance.new('Animation')
Anim.AnimationId = 'rbxassetid://9794130353'
PlayAnim = Character.Humanoid:LoadAnimation(Anim)


UIS.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.Z and toggled == false then
        Character.Humanoid.WalkSpeed = 40
        if not PlayAnim.IsPlaying then
            PlayAnim:Play()
        end
        if toggled == false then
            wait(1.5)
            toggled = true
        end
    end
end)

UIS.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.Z and toggled == true then
        Character.Humanoid.WalkSpeed = 16
        if PlayAnim.IsPlaying then
            PlayAnim:Stop()
        end
        if toggled == true then
            wait(1.5)
            toggled = false
        end
    end
end)
1 Like