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

How do i make this animation stop when the tool is UnEquipped?

Basically the animation wont stop when the tool is UnEquipped so i wanna figure out how to make it stop when the tool is UnEquipped.

tool = script.Parent
tool.Equipped:connect(function()
local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character
 UIS.InputBegan:connect(function(input)--When a player has pressed LeftShift it will play the animation and it will set the normal walking speed (16) to 35.
 if input.KeyCode == Enum.KeyCode.D then
  local Anim = Instance.new('Animation')
  Anim.AnimationId = 'rbxassetid://1297032259'
  PlayAnim = Character.Humanoid:LoadAnimation(Anim)
  PlayAnim:Play()
 end
end)
 
UIS.InputEnded:connect(function(input)
 if input.KeyCode == Enum.KeyCode.D then
  PlayAnim:Stop()
 
tool.Unequipped:connect(function()
    PlayAnim:Stop()
end)
 
 end
end)   
end)

1 answer

✓ Accepted answer

LoadAnimation should only be called once. It's a bad idea to create a new animation object and load it on the humanoid every time the D key is pressed.

local Animation = Instance.new('Animation')
Animation.AnimationId = 'rbxassetid://1297032259'
local Track = Humanoid:LoadAnimation(Animation)

If the Track is created outside of all blocks, you can :Play() it and :Stop() it at any point.

But to answer your main question,

Tool.Unequipped:Connect(function()
	Track:Stop()
end)

1 comment

0

thanks.

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