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)
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.
Forgot Anim and debounce variables.
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!