Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

3 comments

0

When you created your animation, did you save it on Looped? If so, this could be your problem.

0

I saved it on loop so it wouldn't end until the E key was pressed again.

0

I've updated the question. With the new code and gif but still doesn't work

2 answers

✓ Accepted answer

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)

6 comments

0

This script doesn't seem to work. The animation I am using has loop on.

0

does it show anything in output?

0

Here's the gif, https://gyazo.com/db16240e3b4371e813e3e9ca80e288ec. Still keeps the animation. The Script I used was this

0

I'll try your new script now

0

Thank you, It seems to work.

0

ooooh alright :3

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.

3 comments

0

I'll try that now

0

I clicked E again before I unequipped

0

I've updated the question. With the new code and gif but still doesn't work

This discussion is preserved as it was. New answers, comments, and votes are closed.