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

How to stop Animation? Anybody know how to stop animation if tool removed from player backpack?

Asked by 2 years ago
Edited by appxritixn 2 years ago

Anybody know how to stop animation if tool removed from player backpack?

local humanoid = nil
local char = nil
local player = nil
local rootPart = nil
local mouse = nil
local cam = workspace.CurrentCamera
local uis = game:GetService("UserInputService")
local idleAnim = nil
local ForwardAnim = nil
local LeftAnim = nil
local RightAnim = nil


local wPressed = false
local sPressed = false
local aPressed = false
local dPressed = false
local spPressed = false
local equipped = false

local flying = false
script.Parent.Equipped:Connect(function()
    humanoid = script.Parent.Parent.Humanoid
    player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
    char = script.Parent.Parent
    idleAnim = char:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("IdleAnim"))
    ForwardAnim = char:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("ForwardAnim"))
    RightAnim = char:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("ForwardAnim"))
    LeftAnim = char:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("ForwardAnim"))
    rootPart = script.Parent.Parent.HumanoidRootPart
    mouse = player:GetMouse()
    equipped = true
    idleAnim:Play()
end)

script.Parent.Unequipped:Connect(function()
    equipped = false
    idleAnim:Stop()
    ForwardAnim:Stop()
    LeftAnim:Stop()
    RightAnim:Stop()
end)

script.Parent.Remove:Connect(function()
    equipped = false
    idleAnim:Stop()
    ForwardAnim:Stop()
    LeftAnim:Stop()
    RightAnim:Stop()
end)

uis.InputBegan:Connect(function(key, chat)
    if equipped then
        if chat then return end 
        if key.KeyCode == Enum.KeyCode.W and equipped == true then
            wPressed = true
            ForwardAnim:Play()
        elseif key.KeyCode == Enum.KeyCode.S and equipped == true then
            sPressed = true
            ForwardAnim:Play()
        elseif key.KeyCode == Enum.KeyCode.A and equipped == true then
            aPressed = true
            LeftAnim:Play()
        elseif key.KeyCode == Enum.KeyCode.D and equipped == true then
            dPressed = true
            RightAnim:Play()
        end
    end

end)

uis.InputEnded:Connect(function(key, chat)
    if chat then return end
    if key.KeyCode == Enum.KeyCode.W then
        wPressed = false
        ForwardAnim:Stop()
    elseif key.KeyCode == Enum.KeyCode.S then
        sPressed = false
        ForwardAnim:Stop()
    elseif key.KeyCode == Enum.KeyCode.A then
        aPressed = false
        LeftAnim:Stop()
    elseif key.KeyCode == Enum.KeyCode.D then
        dPressed = false
        RightAnim:Stop()
    end
end)
0
It's my first question so it's so bad Krutoi_Emir 0 — 2y
0
Code block updated. appxritixn 2235 — 2y

1 answer

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

Please use code blocks correctly (

-- test

) And don't use Humanoid:LoadAnimation, use Humanoid.Animator:LoadAnimation I don't really understand but you could use: (Remove the --[[]] if you aren't trying to remove the function.)

player.Backpack.ChildRemoved:Connect(function(child)
    local anim = ANIM - your animation to remove
    local toolName = TOOLNAME -- your tool name, ex. if it was "Bat" then put "Bat".
    if child.Name == toolName then
        anim:Stop()
    end
end
--[[
(if you want to remove the connection or remove the function)
local connection = player.Backpack.ChildRemoved:Connect(function(child)
    local anim = ANIM - your animation to remove
    local toolName = TOOLNAME -- your tool name, ex. if it was "Bat" then put "Bat".
    if child.Name == toolName then
        anim:Stop()
    end
end
if [condition] then
    connection:Disconnection()
end
--]]
Ad

Answer this question