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

How to stop animation play after tool is unequipped?

Asked by 5 years ago
01-- R attack
02local Player = game.Players.LocalPlayer
03local Character = Player.Character or script.Parent
04local Humanoid = Character.Humanoid
05local UserInputService = game:GetService("UserInputService")
06local AnimationID = 'rbxassetid://4868889544'
07local key = 'R'
08local debounce = true
09 
10UserInputService.InputBegan:Connect(function(Input, IsTyping)
11    if  IsTyping then return end
12    if Input.KeyCode == Enum.KeyCode[key] and debounce == true then
13        debounce = false
14        local Animation = Instance.new("Animation")
15        Animation.AnimationId = AnimationID
View all 21 lines...

This is the local script in my tool and it plays animation when I press "R", but it still plays animation after too is unequipped, how to fix that? I tried using Tool.equipped event, but I failed and messed up the whole script, my scripting skills are very low.

3 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

The Tool.Equipped() is essential as the player can keep playing the animation without having the Tool equipped.

Try this code.(I have not tested this, so if you have any issues post a comment and I'll try to help you.)

01-- R attack
02local Tool = script.Parent --Put Tool Location here.
03local Player = game.Players.LocalPlayer
04local Character = Player.Character or script.Parent
05local Humanoid = Character.Humanoid
06local UserInputService = game:GetService("UserInputService")
07local AnimationID = 'rbxassetid://4868889544'
08local Animation = Instance.new("Animation")
09Animation.AnimationId = AnimationID
10local LoadAnimation = Humanoid:LoadAnimation(Animation)
11local key = 'R'
12local debounce = true
13 
14Tool.Equipped:Connect(function() -- If the Tool is equipped
15 
View all 32 lines...
Ad
Log in to vote
1
Answered by 5 years ago
1Tool.Unequipped:Connect(function() --Fires when the tool is unequipped
2    Animation:Stop(); --Stops the animation
3end);

You will need to edit it a bit so it works with your script.

Log in to vote
0
Answered by 5 years ago
01-- R attack
02local Player = game.Players.LocalPlayer
03local Character = Player.Character or script.Parent
04local Humanoid = Character.Humanoid
05local UserInputService = game:GetService("UserInputService")
06local AnimationID = 'rbxassetid://4868889544'
07local key = 'R'
08local debounce = true
09 
10UserInputService.InputBegan:Connect(function(Input, IsTyping)
11    if  IsTyping then return end
12    if Input.KeyCode == Enum.KeyCode[key] and debounce == true then
13        debounce = false
14        local Animation = Instance.new("Animation")
15        Animation.AnimationId = AnimationID
View all 22 lines...

Answer this question