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

Why doesn't this play the animation?

Asked by 6 years ago
Edited 6 years ago

I have this localscript:

local tool = script.Parent
local animEvent = tool:WaitForChild("RemoteEvent")
humanoid = nil

animEvent.OnClientEvent:connect(function(...)
    print("a")
    local tuple = {...}
    if tuple[1] == "RunAnimation" then
        print("b")
        local anim = tool:FindFirstChild(tuple[2])
        if anim and humanoid then
            print(anim)
            local loadedanim = humanoid:LoadAnimation(anim)
            if loadedanim then
                print("d")
                loadedanim:Play()
                print(loadedanim)
            end
        end
    end
end)
tool.Equipped:connect(function(mouse)
    humanoid = tool.Parent:FindFirstChild("Humanoid")
end)

and this script:

tool = script.Parent
animEvent = tool:WaitForChild("RemoteEvent")
equipped = nil
character = nil
player = nil
humanoid = nil

tool.Equipped:connect(function(m)
    equipped = true
    character = tool.Parent
    player = game.Players:GetPlayerFromCharacter(character)
    humanoid = character:FindFirstChild("Humanoid")
end)
tool.Unequipped:connect(function()
    equipped = false
    character = nil
    humanoid = nil
end)
tool.Activated:connect(function()
    animEvent:FireClient(player, "RunAnimation", "Hit")
end)

script.Parent.Handle.Touched:connect(function(hit)
    local pl = hit.Parent
    if pl and pl ~= game.Players.LocalPlayer.Character and equipped and humanoid and humanoid.Health > 0 and pl.Humanoid and pl.Humanoid.Health > 0 then
        pl.Humanoid:TakeDamage(10)
    end
end)

But the animation isn't playing. I have added some print stuff which confirms that event code is executing, but the animation still doesn't play. I have looked at this script:

-- Waits for the child of the specified parent
local function WaitForChild(parent, childName)
    while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end
    return parent[childName]
end

local Tool = script.Parent

local Animations = {}
local MyHumanoid
local MyCharacter


local function PlayAnimation(animationName)
    if Animations[animationName] then
        Animations[animationName]:Play()
    end
end

local function StopAnimation(animationName)
    if Animations[animationName] then
        Animations[animationName]:Stop()
    end
end


function OnEquipped(mouse)
    MyCharacter = Tool.Parent
    MyHumanoid = WaitForChild(MyCharacter, 'Humanoid')
    if MyHumanoid then
        Animations['EquipAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'EquipAnim5'))
        Animations['IdleAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'IdleAnim3'))
        Animations['OverheadAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'OverheadAnim2'))
        Animations['SlashAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'SlashAnim2'))
        Animations['ThrustAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'ThrustAnim2'))
        Animations['UnequipAnim'] = MyHumanoid:LoadAnimation(WaitForChild(Tool, 'UnequipAnim2'))
    end
    PlayAnimation('EquipAnim')
    PlayAnimation('IdleAnim')
end

function OnUnequipped()
    for animName, _ in pairs(Animations) do
        StopAnimation(animName)
    end
end

Tool.Equipped:connect(OnEquipped)
Tool.Unequipped:connect(OnUnequipped)

WaitForChild(Tool, 'PlaySlash').Changed:connect(
    function (value)
        --if value then
            PlayAnimation('SlashAnim')
        --else
        --  StopAnimation('SlashAnim')
        --end
    end)

WaitForChild(Tool, 'PlayThrust').Changed:connect(
    function (value)
        --if value then
            PlayAnimation('ThrustAnim')
        --else
        --  StopAnimation('ThrustAnim')
        --end
    end)

WaitForChild(Tool, 'PlayOverhead').Changed:connect(
    function (value)
        --if value then
            PlayAnimation('OverheadAnim')
        --else
        --  StopAnimation('OverheadAnim')
        --end
    end)

But that doesn't seem any different from my script, but my script doesn't work while that one does. What is wrong here?

Edit for hiimgoodpack: Do you mean like this?:

local tool = script.Parent
local animEvent = tool:WaitForChild("RemoteEvent")
humanoid = nil

animEvent.OnClientEvent:connect(function(...)
    print("a")
    local tuple = {...}
    if tuple[1] == "RunAnimation" then
        print("b")
        local anim = tool:FindFirstChild(tuple[2])
        if anim and humanoid then
            print(anim)
            local loadedanim = humanoid:LoadAnimation(anim)
            if loadedanim then
                print("d")
                loadedanim:Play()
                print(loadedanim)
            end
        end
    end
end)
tool.Equipped:connect(function(mouse)
    humanoid = tool.Parent:FindFirstChild("Humanoid")
    if humanoid then
        humanoid = tool.Parent:FindFirstChild("Humanoid").Animator
    end
end)

Because that's not working either.

Edit 2: When I check its "IsPlaying" property after I play the animation like this:

if loadedanim then
                print("d")
                loadedanim:Play()
                print(loadedanim.IsPlaying)
            end

I get "true". I also checked its "Length" property and it's "1", but still nothing happens even though it seems like it's playing. ???

0
You need their Animator, not the humanoid. hiimgoodpack 2009 — 6y
0
Do you mean like in the edit hiimgoodpack? therealviklo 0 — 6y
0
? hiimgoodpack 2009 — 6y
0
loading the animation with the animator doesn't work either therealviklo 0 — 6y
View all comments (2 more)
0
"anim" is not defined. hiimgoodpack 2009 — 6y
0
what? isn't it defined on line 10? therealviklo 0 — 6y

Answer this question