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

How do I stop my animations from repeating?

Asked by 2 years ago

I've been working on the script for a while, I kind of know the issue, but I cant quite put my finger on it. Any help would be great.

Tool = script.Parent
char = Tool:FindFirstAncestorWhichIsA("Player").Character
hum = char:WaitForChild("Humanoid")
animfolder = Tool.Anims


slash = animfolder.Slash
idle = animfolder.Idle
equip = animfolder.Equip
unequip = animfolder.Unequip
swordwalk = animfolder.Walk
swordrun = animfolder.Run

playerState = nil
tag = Tool.Tag
slashsound = Tool.Handle.Slash
hitsound = Tool.Handle.Hit

cd = false

equipped = false

function Hit_Target(character, target_humanoid)
    --print(character, target_humanoid)
    target_humanoid:TakeDamage(20)
    hitsound:Play()
end

function Equipped()
    equipped = true
    walkanim = hum:LoadAnimation(swordwalk)
    idleanim = hum:LoadAnimation(idle)
    runanim = hum:LoadAnimation(swordrun)
    print("Equipped")   
end

function Activated()
    --print("Slash!")
    if cd == false then
        cd = true
        local animationTrack = hum:LoadAnimation(slash)
        idleanim:Stop()
        animationTrack:Play(0.3,1,1.5)
        slashsound:Play()

        Tool.Handle.Touched:Connect(function(hit)
            if cd == true then
                --print(hit)
                local character = hit:FindFirstAncestorWhichIsA("Model")
                if character then
                    if character:FindFirstChildWhichIsA("Humanoid") then
                        local target_humanoid = character:FindFirstChildWhichIsA("Humanoid")

                        if tag.Value ~= nil then
                            print("Already Tagged")

                        else
                            tag.Value = target_humanoid
                            --print(character.Name, target_humanoid.Name)
                            Hit_Target(character, target_humanoid)
                        end
                    end
                end
            end
        end)

        wait(1)
        cd = false
        tag.Value = nil
        idleanim:Play()

    end
end

function Deactivated()
    print("Deactivated")
end

function Unequipped()
    equipped = false
    print("Unequipped")
    idleanim:Stop()
end

Tool.Equipped:Connect(Equipped)
Tool.Activated:Connect(Activated)
Tool.Deactivated:Connect(Deactivated)
Tool.Unequipped:Connect(Unequipped)

while true do 
    wait(1)
    if equipped == true then
        print(hum:GetPlayingAnimationTracks())

        --for i, v in pairs(hum:GetPlayingAnimationTracks()) do
        --  print(i, v)
        --  if v.Name ~= playerState then
        --      v:Stop()
        --  end
        --end
        local speed = Vector3.new(char.HumanoidRootPart.Velocity.X, 0, char.HumanoidRootPart.Velocity.Z).Magnitude
        if speed >= 1 and speed <= 16.1 then
            print("Walking")
            print(hum:GetPlayingAnimationTracks())
            if table.find(hum:GetPlayingAnimationTracks(), "Walk") then
            else
                idleanim:Stop()
                runanim:Stop()
                walkanim:Play()
            end
            playerState = "Walk"
        elseif speed == 0 then
            print("idle")
            print(hum:GetPlayingAnimationTracks())
            if table.find(hum:GetPlayingAnimationTracks(), "Idle") then
            else
                runanim:Stop()
                walkanim:Stop()
                idleanim:Play()
            end
            playerState = "Idle"
        else
            print("Running")
            if table.find(hum:GetPlayingAnimationTracks(), "Run") then
            else
                idleanim:Stop()
                walkanim:Stop()
                runanim:Play()
            end
            playerState = "Run"
        end
    end
end

1 answer

Log in to vote
0
Answered by 2 years ago

Hey :GetPlayingAnimationTracks() is deprecated so try and use .IsPlaying instead. Also you are looking for a "Run","Walk","Idle" that doesnt exist as :GetPlayingAnimationTracks() is a table of Objects and not actually strings.

Ad

Answer this question