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

[UNSOLVED] Animation won't stop working even after i unequipped the tool?

--//Services
local UIS = game:GetService('UserInputService')

--//Variables
local player = game.Players.LocalPlayer
local char = player.Character
local tool = script.Parent


--//Use this since it's a local script inside a tool
player.CharacterAdded:Connect(function(character)
char = character
local hum = char:WaitForChild('Humanoid')


--//Animation
local animation = Instance.new('Animation')
animation.AnimationId = "rbxassetid://2680328238"
local spinSlash = hum:LoadAnimation(animation)

--//Equipped
tool.Equipped:Connect(function()
	
--//Input
local debounce = false
UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.X and not debounce then
		debounce = true
		spinSlash:Play()
		wait(2)
		debounce = false
		end
	end)
end)

--//Unequipped
tool.Unequipped:Connect(function()
	spinSlash:Stop()
	end)
end)

This is an animation of a sword slashing so it's not a looped animations. When the tool is equipped, x can be pressed in order to play the slashing animation but when i unequip it, x can still be pressed to play the animation again. How can i fix this so that x can only be pressed when the tool is equipped?

4 comments

0

make a variable for the :LoadAnimation() at the beginning of the script, and call :Start() and :Stop() on it, rather than reloading it everytime

0

If i made the variable local spinSlash = hum:LoadAnimation(tool.SpinSlash) at the top, wouldn't it be the same as just reloading it because the variable is still loading the animation.

0

have you tried setting debounce to false after unequipping?

0

I just tried that and it didn't make a difference because debounce is already set to false after the animation played

2 answers

You are loading another animation when you are unequipping the tool. Fixed script:


local UIS = game:GetService('UserInputService')
local player = game.Players.LocalPlayer
local char = player.Character
local tool = script.Parent
--//Use this since it's a local script inside a tool
player.CharacterAdded:Connect(function(character)
char = character
local hum = char:WaitForChild('Humanoid')
 local meme = hum:LoadAnimation(tool.SpinSlash)
--//Equipped
tool.Equipped:Connect(function()
 
 
--//Input
local debounce = false
UIS.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.X and not debounce then
        debounce = true
 
        meme:Play()
        wait(2)
        debounce = false
        end

    end)

end)

 

--//Unequipped

tool.Unequipped:Connect(function()

    meme:Stop()
    end)
end)

10 comments

0

It didn't work

0

did you get any errors?

0

nope

0

Isn't creating the variable at the top of the script the same as just loading the animation again?

0

No , because if you load another one , you aren't stopping the first one but the animation that you just loaded.

0

Well i edited my code and it doesn't seem to be working and there are no signs of error

0

I tried using the code above and it seems to work perfectly fine.

0

You sure the animation doesn't load when you press x with the tool unequipped? Are you using server script or local script because i really have no idea why my script isn't working,

0

I'm using a

0

LocalScript**

NVM, go with the other answer!

It is because you have

UIS.InputBegan:Connect(function(input)

as a form of global function. It isn't put in any other bounds of another function (as a local) like within the function, "equipped" but is defined as soon as the script is ran. So this means once you load in, you could press "X" by theory and the script would run again.

Since I don't exactly understand how you could make it local within equipped with it working properly and don't feel for researching at the moment, I will provide a top-of-the-head solution.

Have,

UIS.InputBegan:Connect(function(input)

As a defined function like:

function inputActivated(input)
	-- code
end

UIS.InputBegan:Connect(inputActivated)

Then when unequipped occurs, do this:

tool.Unequipped:Connect(function()
	inputActivated = nil
	hum:LoadAnimation(tool.SpinSlash):Stop()
	end
end)

You also had an additional "end)" that MIGHT'VE caused a problem.

tool.Unequipped:Connect(function()
	hum:LoadAnimation(tool.SpinSlash):Stop()
	end) -- Here
end)

Sorry for my laziness, but hopefully that helps.

3 comments

0

The end) is from the CharacterAdded event

0

OH

0

But yeah, probably should go for the other answer then lol -- if it did work idk, I aint one that is knowledgable about animations 'n stuff

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