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

Need help with my animation not stopping, Anyone able to help?

Asked by 4 years ago
Edited 4 years ago

In my tool I've got a local script which runs an animation when holding the tool and the key is pressed. In the tool there is the Animation folder with the animations and there is the handle and local script. The Problem I have is when I press the key the animation plays but when I click the key again it doesn't stop playing.

Here is my script

local player = script.Parent.Parent.Parent --Gets the player if this is a regular tool in starterpack
local character = player.Character or player.CharacterAdded:Wait()
local tool = script.Parent
local mouse = player:GetMouse()
local UserInputService = game:GetService("UserInputService")

-- Bools --
local toolon = false
local isblocking = false

-- Animations --
local Block = script.Parent.Animations.Block
local animationTrack

tool.Equipped:Connect(function()
    toolon = true
end)

tool.Unequipped:Connect(function()
    toolon = false
end)

UserInputService.InputBegan:connect(function(i,g)
    if i.UserInputType == Enum.UserInputType.Keyboard then
        if i.KeyCode == Enum.KeyCode.E and toolon == true and isblocking == false then
            animationTrack = character:WaitForChild("Humanoid"):LoadAnimation(Block)
            animationTrack:Play()
            print("Block Is On")
            isblocking = true
        end
    end
end)

UserInputService.InputBegan:connect(function(i,g)
    if i.UserInputType == Enum.UserInputType.Keyboard then
        if i.KeyCode == Enum.KeyCode.E and toolon == true and isblocking == true then
            animationTrack:Stop()
            print("Block Is Off")
            isblocking = false
        end
    end
end)

UPDATE: I've edited the code and changed it but it still doesn't work, Here is the new gif https://gyazo.com/985aef884e981d7318ce9fbd149f5757 Here is the new Rbxl https://mega.nz/file/nNxRTZRB#N_XZwyc8_QxjP0BQZdjCxdjxI6a_sPOCHEz_7JViynU

0
When you created your animation, did you save it on Looped? If so, this could be your problem. Channy97 10 — 4y
0
I saved it on loop so it wouldn't end until the E key was pressed again. kyloren528 11 — 4y
0
I've updated the question. With the new code and gif but still doesn't work kyloren528 11 — 4y

2 answers

Log in to vote
0
Answered by
Necro_las 412 Moderation Voter
4 years ago
Edited 4 years ago

You are loading the animation only inside the input function. It will work if you call LoadAnimation outside the functions so both of them can work with that same animation, like this:

local variables =  etc

-- Animations --
local Block = script.Parent.Animations.Block
local animationTrack = character:WaitForChild("Humanoid"):LoadAnimation(Block)

UserInputService.InputBegan:connect(function(i,g)
    if i.UserInputType == Enum.UserInputType.Keyboard then
        if i.KeyCode == Enum.KeyCode.E and toolon == true and isblocking == false then
            animationTrack:Play()
            print("Block Is On")
            isblocking = true
        end
    end
end)

UserInputService.InputBegan:connect(function(i,g)
    if i.UserInputType == Enum.UserInputType.Keyboard then
        if i.KeyCode == Enum.KeyCode.E and toolon == true and isblocking == true then
            animationTrack:Stop()
            print("Block Is Off")
            isblocking = false
        end
    end
end)

JUST TO SIMPLIFY YOUR CODE WRITING:

UserInputService.InputBegan:connect(function(i,g)
    if i.UserInputType == Enum.UserInputType.Keyboard -- and
    and i.KeyCode == Enum.KeyCode.E and toolon == true and isblocking == false then
        animationTrack:Play()
        print("Block Is On")
        delay(0.1, function() isblocking = true end) -- prevents from making this script crazy.

    elseif i.UserInputType == Enum.UserInputType.Keyboard -- only one connection to input
        and i.KeyCode == Enum.KeyCode.E and toolon == true and isblocking == true then
        animationTrack:Stop()
        print("Block Is Off")
        delay(0.1, function() isblocking = false end)
    end
end)
0
This script doesn't seem to work. The animation I am using has loop on. kyloren528 11 — 4y
0
does it show anything in output? Necro_las 412 — 4y
0
Here's the gif, https://gyazo.com/db16240e3b4371e813e3e9ca80e288ec. Still keeps the animation. The Script I used was this kyloren528 11 — 4y
0
I'll try your new script now kyloren528 11 — 4y
View all comments (2 more)
0
Thank you, It seems to work. kyloren528 11 — 4y
0
ooooh alright :3 Necro_las 412 — 4y
Ad
Log in to vote
0
Answered by
zadobyte 692 Moderation Voter
4 years ago
Edited 4 years ago

It's generally better practice to use UserInputService but what I'd suggest is separate the if statements instead of using elseif. Usually when I have a problem with elseif, I solve it by making them two separate if statements.

Edit: You never stopped the animation when the tool was unequipped so I'm pretty sure that's a result.

0
I'll try that now kyloren528 11 — 4y
0
I clicked E again before I unequipped kyloren528 11 — 4y
0
I've updated the question. With the new code and gif but still doesn't work kyloren528 11 — 4y

Answer this question