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 4 years ago
-- R attack
local Player = game.Players.LocalPlayer
local Character = Player.Character or script.Parent
local Humanoid = Character.Humanoid
local UserInputService = game:GetService("UserInputService")
local AnimationID = 'rbxassetid://4868889544'
local key = 'R'
local debounce = true

UserInputService.InputBegan:Connect(function(Input, IsTyping)
    if  IsTyping then return end
    if Input.KeyCode == Enum.KeyCode[key] and debounce == true then
        debounce = false
        local Animation = Instance.new("Animation")
        Animation.AnimationId = AnimationID
        local LoadAnimation = Humanoid:LoadAnimation(Animation)
        LoadAnimation:play()
        wait(5)
        debounce = true
        end
    end)

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 4 years ago
Edited 4 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.)

-- R attack
local Tool = script.Parent --Put Tool Location here.
local Player = game.Players.LocalPlayer
local Character = Player.Character or script.Parent
local Humanoid = Character.Humanoid
local UserInputService = game:GetService("UserInputService")
local AnimationID = 'rbxassetid://4868889544'
local Animation = Instance.new("Animation")
Animation.AnimationId = AnimationID
local LoadAnimation = Humanoid:LoadAnimation(Animation)
local key = 'R'
local debounce = true

Tool.Equipped:Connect(function() -- If the Tool is equipped

    UserInputService.InputBegan:Connect(function(Input, IsTyping) -- If the player presses the key while the Tool is equipped then it will run the code below.
        if  IsTyping then return end

        if Input.KeyCode == Enum.KeyCode[key] and debounce == true then
            debounce = false
            LoadAnimation:Play()
            wait(5)
            debounce = true
        end
    end)
end)

Tool.Unequipped:Connect(function() -- If the player has unequipped the Tool, we want to stop the animation in case they unequip the Tool during the Animation.

    LoadAnimation:Stop()

end)

Ad
Log in to vote
1
Answered by 4 years ago
Tool.Unequipped:Connect(function() --Fires when the tool is unequipped
    Animation:Stop(); --Stops the animation
end);

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

Log in to vote
0
Answered by 4 years ago
-- R attack
local Player = game.Players.LocalPlayer
local Character = Player.Character or script.Parent
local Humanoid = Character.Humanoid
local UserInputService = game:GetService("UserInputService")
local AnimationID = 'rbxassetid://4868889544'
local key = 'R'
local debounce = true

UserInputService.InputBegan:Connect(function(Input, IsTyping)
    if  IsTyping then return end
    if Input.KeyCode == Enum.KeyCode[key] and debounce == true then
        debounce = false
        local Animation = Instance.new("Animation")
        Animation.AnimationId = AnimationID
        local LoadAnimation = Humanoid:LoadAnimation(Animation)
        LoadAnimation:Play()
        wait(1.5) -- anim length
    LoadAnimation:Stop()
        debounce = true
        end
end)

Answer this question