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

Trying to play animation and stop the player from moving. Solution?

Asked by 5 years ago

I am trying to make a game where you collect candies, and when you touch them, you stop moving and run an animation to collect the candy. I've managed to make the animation and spawn candies, but I looked everywhere and every solution I've tried to make the player stop moving didn't work.

What do I need to add to this script that belongs to the candies (parts) to make the player stop moving, and when the animation is done, let the player move again? (the script is working)

local Anim = script.Parent.GetCandy
local debounce = false

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and debounce == false then
        debounce = true
        local AnimTrack = hit.Parent.Humanoid:LoadAnimation(Anim)
        AnimTrack:Play()
        wait(.9)
        script.Parent:Destroy()
        wait(1.6)
        debounce = false
    end
end)

2 answers

Log in to vote
1
Answered by
xPolarium 1388 Moderation Voter
5 years ago
Edited 5 years ago

You can use the Humanoid's WalkSpeed to completely stop their movement then reset it back to default 16. Instead of using wait()'s in the code you could also use the Stopped() event of the AnimTrack to yield the code.

local candy = script.Parent

local Anim = script.Parent.GetCandy

local debounce = false

candy.Touched:Connect(function(hit)
    local human = hit.Parent:FindFirstChildOfClass("Humanoid")

    if human then
        if debounce then return end --similar debounce to your original
        debounce = true

        human.WalkSpeed = 0

        local animTrack = human:LoadAnimation(Anim)
        animTrack:Play()

        animTrack.Stopped:Wait() --Wait until the event 'Stopped' fires

        human.WalkSpeed = 16

        print("Collected candy")

        candy:Destroy()
    end
end)

RBXScriptSignal:Wait() helps us yield until the animation is completed. We then destroy the candy since it's no longer needed. Although you would add to the player's collected candy values before this.

References:

Edit:

Forgot Anim and debounce variables.

0
The only thing is the wait() functions are to make the candy disappear in the middle of the animation, but this will do it. AradIsHere 2 — 5y
0
It seems as the animation never stopped, so I'm stuck in place. Sorry for late response AradIsHere 2 — 5y
0
You probably have the animation on a loop. Check the setting in the Animation Editor. I've also tested this with a non-looping anim too. xPolarium 1388 — 5y
0
It's not on a loop, believe me. It seems like the animation "never ends" so the speed never gets changed back. AradIsHere 2 — 5y
0
So I just brought back the wait()'s. AradIsHere 2 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

I found the solution. I brought back the wait commands and only added the humanoid speed. It works, and your script doesn't for some reason maybe because the animation "never ends", but thanks for the help!

Answer this question