01 | -- R attack |
02 | local Player = game.Players.LocalPlayer |
03 | local Character = Player.Character or script.Parent |
04 | local Humanoid = Character.Humanoid |
05 | local UserInputService = game:GetService( "UserInputService" ) |
06 | local AnimationID = 'rbxassetid://4868889544' |
07 | local key = 'R' |
08 | local debounce = true |
09 |
10 | UserInputService.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 |
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.
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 |
02 | local Tool = script.Parent --Put Tool Location here. |
03 | local Player = game.Players.LocalPlayer |
04 | local Character = Player.Character or script.Parent |
05 | local Humanoid = Character.Humanoid |
06 | local UserInputService = game:GetService( "UserInputService" ) |
07 | local AnimationID = 'rbxassetid://4868889544' |
08 | local Animation = Instance.new( "Animation" ) |
09 | Animation.AnimationId = AnimationID |
10 | local LoadAnimation = Humanoid:LoadAnimation(Animation) |
11 | local key = 'R' |
12 | local debounce = true |
13 |
14 | Tool.Equipped:Connect( function () -- If the Tool is equipped |
15 |
1 | Tool.Unequipped:Connect( function () --Fires when the tool is unequipped |
2 | Animation:Stop(); --Stops the animation |
3 | end ); |
You will need to edit it a bit so it works with your script.
01 | -- R attack |
02 | local Player = game.Players.LocalPlayer |
03 | local Character = Player.Character or script.Parent |
04 | local Humanoid = Character.Humanoid |
05 | local UserInputService = game:GetService( "UserInputService" ) |
06 | local AnimationID = 'rbxassetid://4868889544' |
07 | local key = 'R' |
08 | local debounce = true |
09 |
10 | UserInputService.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 |