-
What do you want to achieve?
I want to stop all animations when a tool is unequipped. (So idle, walking, jumping animations etc can run) -
What is the issue?
When I unequip the tool, the holding animation still plays. -
What solutions have you tried so far?
I have tried calling theStop
method on the animation track for the holding animation in theonUnequipped
function, but it doesn’t seem to be working.
Here is my current code:
local Tool = script.Parent
local enabled = true
local pickUpAnim = Tool.Animations:WaitForChild("Tool PickUp")
local holdingAnim = Tool.Animations:WaitForChild("holding")
local unequipAnim = Tool.Animations:WaitForChild("Tool Unequip")
local animTrack1 = nil
local animTrack2 = nil
function onActivated()
local player = game:GetService("Players"):GetPlayerFromCharacter(Tool.Parent)
local thirst = player:WaitForChild("playerstats"):WaitForChild("moods"):WaitForChild("Thirst")
local thirstIncrease = 17
local timesDrinked = Tool:WaitForChild("TimesDrinked")
if not enabled or timesDrinked.Value >= 3 then
return
end
enabled = false
Tool.GripForward = Vector3.new(0,-.759,-.651)
Tool.GripPos = Vector3.new(1.5,-.5,.3)
Tool.GripRight = Vector3.new(1,0,0)
Tool.GripUp = Vector3.new(0,.651,-.759)
Tool.Handle.DrinkSound:Play()
if timesDrinked.Value >= 3 then
Tool:Destroy()
else
task.wait(3)
local h = Tool.Parent:FindFirstChild("Humanoid")
if h ~= nil then
if h.MaxHealth > h.Health + 5 then
h.Health = h.Health + 5
else
h.Health = h.MaxHealth
end
end
Tool.GripForward = Vector3.new(-.976,0,-0.217)
Tool.GripPos = Vector3.new(0.03,0,0)
Tool.GripRight = Vector3.new(.217,0,-.976)
Tool.GripUp = Vector3.new(0,1,0)
end
task.wait(1.5)
-- Increase the player's thirst by the specified amount if it is not already at the maximum of 100
if thirst.Value < 100 then
thirst.Value = math.min(thirst.Value + thirstIncrease, 100)
end
-- Increase timesDrinked by 1
timesDrinked.Value = timesDrinked.Value + 1
enabled = true
end
function onEquipped()
Tool.Handle.OpenSound:Play()
local pickUpAnim = Tool.Animations:WaitForChild("Tool PickUp")
local holdingAnim = Tool.Animations:WaitForChild("holding")
local humanoid = Tool.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
-- Only load the animation tracks if they don't already exist
if not animTrack1 then
animTrack1 = animator:LoadAnimation(pickUpAnim)
end
if not animTrack2 then
animTrack2 = animator:LoadAnimation(holdingAnim)
end
-- Play the pickUp animation once
animTrack1:Play()
animTrack1.Stopped:Wait()
-- Play the holding animation
animTrack2:Play()
end
function onUnequipped()
local unequipAnim = Tool.Animations:WaitForChild("Tool Unequip")
local humanoid = Tool.Parent:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local animTrack = animator:LoadAnimation(unequipAnim)
-- Stop the pickUpAnim and holdingAnim animations
if animTrack1 then
animTrack1:Stop()
end
if animTrack2 then
animTrack2:Stop()
end
animTrack:Play()
task.wait(0.5)
animTrack.Stopped:Wait()
end
Tool.Activated:Connect(onActivated)
Tool.Equipped:Connect(onEquipped)
Tool.Unequipped:Connect(onUnequipped)