Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

My animation will not stop once i press the keybind how can i fix this?

Asked by
liynux 0
4 years ago

when i press w the animation plays but does not stop.

local Player = game.Players.LocalPlayer
local Character = Player.Character or script.Parent
local Humanoid = Character.Humanoid
local UserInputService = game:GetService("UserInputService")
local AnimationId = 'rbxassetid://4397242217'
local Key = 'W'

UserInputService.InputBegan:Connect(function(Input, ItTyping)
    if ItTyping then return end
    if Input.KeyCode == Enum.KeyCode[Key] then
        local Animation = Instance.new("Animation")
        Animation.AnimationId = AnimationId
        local LoadAnimation = Humanoid:LoadAnimation(Animation)
        LoadAnimation:Play()

    end
end)

UserInputService.InputEnded:Connect(function(Input, IsTyping)
    if IsTyping then return end
    if Input.KeyCode == Enum.KeyCode[Key] then
        local Animation = Instance.new("Animation")
        Animation.AnimationId = AnimationId
        local LoadAnimation = Humanoid:LoadAnimation(Animation)
        LoadAnimation:Stop()
    end
end)

1 answer

Log in to vote
0
Answered by 4 years ago

The reason its not stopping is because in the end you're creating a new one then stopping it so this is how to fix.

local Player = game.Players.LocalPlayer
local Character = Player.Character or script.Parent
local Humanoid = Character.Humanoid
local UserInputService = game:GetService("UserInputService")
local AnimationId = 'rbxassetid://4397242217'
local Key = 'W'

UserInputService.InputBegan:Connect(function(Input, ItTyping)
    if ItTyping then return end
    if Input.KeyCode == Enum.KeyCode[Key] then
        local Animation = Instance.new("Animation")
        Animation.AnimationId = AnimationId
        local LoadAnimation = Humanoid:LoadAnimation(Animation)
        LoadAnimation:Play()
    Animation.Parent = Character
    Animation.name = "Animation"
    end
end)

UserInputService.InputEnded:Connect(function(Input, IsTyping)
    if IsTyping then return end
        if Input.KeyCode == Enum.KeyCode[Key] then
        if Character:FindFirstChild("Animation") == true then
            Humanoid:LoadAnimation(Character:FindFirstChild("Animation")):Stop()
        end
    end
end)
0
i didn't test it so good luck HappyTimIsHim 652 — 4y
0
ok thank you will try it liynux 0 — 4y
0
what do i put in the animation.name par because it is saying it is an invalid member of animation liynux 0 — 4y
0
i figured out the problem but it dtill did not work thank you anyways liynux 0 — 4y
Ad

Answer this question