Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Starting the animation is fine, but I'm having trouble stopping my looped animation?

Hello, for my game, I was making water for the player to swim on. I made an animation, i tested it, and it works. It is set to loop. I made a script a child of the water brick, and this is what i added to it:

wait(1)
local swimming = Instance.new("Animation")
local player = game.Players.LocalPlayer.Character
swimming.AnimationId = "rbxassetid://729824035"
script.Parent.Touched:connect(function(anim)
	player.Humanoid:LoadAnimation(swimming):Play()
	if script.Parent.Touched == false then
		player.Humanoid:LoadAnimation(swimming):Stop()
		
	end
end)

I'm not sure if i did something incorrectly, but the animation does play when i am in the water, but does not stop when i get out of the water. I'm sort of new to combining scripts with animations. Any help will be appreciated! :)

1 answer

✓ Accepted answer

Make sure this script is inside of the part you want. I left some comments for you to read.

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild('Humanoid') then
        local swimming = Instance.new("Animation")
        swimming.AnimationId = "rbxassetid://729824035"
        swimming.Parent = character.Humanoid
      	playAnim = character.Humanoid:LoadAnimation(swimming) --Make global variable so that wa we can use the "Stop()" function to stop the animation.
      	playAnim:Play() --plays animation
    end
end)

script.Parent.TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild('Humanoid') then --check if whatever stopped touching has a humanoid
      	playAnim:Stop() --stops animation
	end
end)

If this helped, please accept and upvote my answer :)

This discussion is preserved as it was. New answers, comments, and votes are closed.