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

Why isn't the animation being looped?

Asked by 4 years ago
local debounce = true
script.Parent.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChild("Humanoid")

    if humanoid then
        if debounce == true then
            print('okay')
            humanoid.WalkSpeed = 0
            humanoid.JumpPower = 0
            local torso = hit.Parent.LowerTorso
            torso.CFrame = script.Parent.CFrame * CFrame.new(0,0,0)
            local weld = Instance.new("Weld")
            weld.Part0 = script.Parent
            weld.C0 = script.Parent.CFrame:Inverse()
            weld.Part1 = torso
            weld.C1 = torso.CFrame:Inverse()
            weld.Parent = script.Parent
            script.Parent.Anchored = false
            --[==[ This is weld 
                this comment is helping separate the two, okay.
            --]==]
            local animation = Instance.new("Animation")
            animation.AnimationId = "rbxassetid://3267377176"
            local animationTrack = humanoid:LoadAnimation(animation)
            animationTrack.Looped = true
            animationTrack:Play()
            debounce = false
        end
    end
end)

script.Parent.TouchEnded:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid then
        debounce = true
    end
end)

The animation is basically just to lay you down in a tube, I've used the looped property but isn't working. Any help is appreciated.

2 answers

Log in to vote
0
Answered by
memguy 161
4 years ago
Edited 4 years ago

You have to set AnimationTrack properties AFTER you've played it. So the code should look as follows:

local debounce = true
script.Parent.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChild("Humanoid")

    if humanoid then
        if debounce == true then
            print('okay')
            humanoid.WalkSpeed = 0
            humanoid.JumpPower = 0
            local torso = hit.Parent.LowerTorso
            torso.CFrame = script.Parent.CFrame * CFrame.new(0,0,0)
            local weld = Instance.new("Weld")
            weld.Part0 = script.Parent
            weld.C0 = script.Parent.CFrame:Inverse()
            weld.Part1 = torso
            weld.C1 = torso.CFrame:Inverse()
            weld.Parent = script.Parent
            script.Parent.Anchored = false
            --[==[ This is weld 
                this comment is helping separate the two, okay.
            --]==]
            local animation = Instance.new("Animation")
            animation.AnimationId = "rbxassetid://3267377176"
            local animationTrack = humanoid:LoadAnimation(animation)
            animationTrack:Play()
            animationTrack.Looped = true
            debounce = false
        end
    end
end)

script.Parent.TouchEnded:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid then
        debounce = true
    end
end)
0
(Edited since I didn't know how to highlight syntax. memguy 161 — 4y
Ad
Log in to vote
0
Answered by
sleazel 1287 Moderation Voter
4 years ago
Edited 4 years ago

I am not sure why Your animation is not looping, but I can tell you that Your script is broken. You should never, ever rely on TouchEnded for humanoids. Even though you set Walk and JumpPower to 0, your animation is causing new events. What is really happening in Your case, that both events fire multiple times, and while touched has a debounce, TouchEnded does not. Therefore you are loading dozens of animations to the same character. What is worse, there is no guarantee that these events will fire same amount of times, so adding a counter will not help (believe me I tried)

Better solution in Your case would be a table of characters, which already have amnimation loaded and replacing touch ended with magnitude check. That is what I use and it always works perfectly. What you basically do is load animation once and next time touched is trigerred, You just play it (assuming you will stop it on touch ended).

I am not sure when or how you are planning to restrore the WalkSpeed and JumpPower, but I will add a 5 seconds period, so I can show you how to reliably detect touch ended.


local charactersAnimations = {} local nearbyCharacters = {} --below code needs to be run just once local animation = Instance.new("Animation") animation.AnimationId = "rbxassetid://3267377176" script.Parent.Touched:Connect(function(hit) local humanoid = hit.Parent:FindFirstChild("Humanoid") if humanoid then if nearbyCharacters[hit.Parent] == nil then nearbyCharacters[hit.Parent] = true print('okay') humanoid.WalkSpeed = 0 humanoid.JumpPower = 0 local torso = hit.Parent.LowerTorso torso.CFrame = script.Parent.CFrame * CFrame.new(0,0,0) local weld = Instance.new("Weld") weld.Part0 = script.Parent weld.C0 = script.Parent.CFrame:Inverse() weld.Part1 = torso weld.C1 = torso.CFrame:Inverse() weld.Parent = script.Parent script.Parent.Anchored = false --[==[ This is weld this comment is helping separate the two, okay. --]==] if charactersAnimations[hit.Parent] == nil then charactersAnimations[hit.Parent] = humanoid:LoadAnimation(animation) charactersAnimations[hit.Parent].Looped = true end charactersAnimations[hit.Parent]:Play() wait (5) --restore -added just to show you how magnitude check works. Feel free to use Your own restore system humanoid.WalkSpeed = 16 --default humanoid.JumpPower = 50 --default while (script.Parent.Position - hit.Parent.HumanoidRootPart.Position).magnitude < 10 do --adjust if necessary, checks distance from part to character wait(1) end nearbyCharacters[hit.Parent] = nil charactersAnimations[hit.Parent]:Stop() end end end)

Answer this question