Stopping an animation when a tool is unequipped or changed during an activity

Making a gathering system, starting with cutting a tree through proximity prompts. Wanting to make this script make the animation stop when the player either unequips the required tool. As the script runs the 5 second animation for cutting the tree when they unequip or change tools.

I assumed the else statement would cause the script to trigger when they are not using the required “hatchet” tool.

local prompt = script.Parent
local animation = game.ReplicatedStorage.Animations.Hatchet.swing1

prompt.Triggered:Connect(function(player)
	local human = player.Character:WaitForChild("Humanoid")
	local loadAnimation = human.Animator:LoadAnimation(animation)
	local playermodel = player.Character:WaitForChild("HumanoidRootPart")
	
	if player.Character:FindFirstChild("Hatchet") and player.Character["Hatchet"]:IsA("Tool") then
		print(player.Name, "is gathering resources.")
		playermodel.CFrame = CFrame.lookAt(playermodel.Position, Vector3.new(prompt.Parent.Position.X, playermodel.Position.Y, prompt.Parent.Position.Z))
		prompt.Enabled = false	
		human.WalkSpeed = 0
		loadAnimation:Play()
	
	wait(5)
		
		prompt.Enabled = true
		human.WalkSpeed = 18
		
	else
		player.PlayerGui.NoTool.Enabled = true
		player.PlayerGui.NoTool.NoHatchet.Visible = true
		print("You do not have a hatchet.")
		loadAnimation:Stop()
		
	wait(2)
		
		player.PlayerGui.NoTool.Enabled = false
		player.PlayerGui.NoTool.NoHatchet.Visible = false
	end
end)

Eventually I will want to add resources to a leaderboard when people gather but for now, I’m just working on the basics of the script.

1 Like

replace wait(5) with

local twait = 5
local start = tick()
repeat task.wait(0.1)
    until tick() - start > twait or not player.Character:FindFirstChild("Hatchet")
-- stop the anim

You could probably just do this.

if player.Character:FindFirstChild("Hatchet") and player.Character.Hatchet:IsA("Tool") then
	--- code for animation
elseif player.Backpack:FindFirstChild("Hatchet") then
	--- code for stopping animation
end

Also if your animation is 5 seconds long then you can use

the OP wanted to CANCEL the 5 second animation if the tool is unequipped during it, not check if the tool is equipped

Ohh so they want to make the code for chopping tree down stop if its unequipped while its playing. Maybe this might work??

if player.Character:FindFirstChild("Hatchet") and player.Character.Hatchet:IsA("Tool") then
	--- code for animation
	local human = player.Character:WaitForChild("Humanoid")
	local loadAnimation = human.Animator:LoadAnimation(animation) --  what you defined animation as
	player.Character.ChildRemoved:Connect(function(child)
		if child.Name == "Hatchet" and child:IsA("Tool") and loadAnimation.IsPlaying then
			-- code for stopping animation and chopping tree
		end
	end)
	
elseif player.Backpack:FindFirstChild("Hatchet") then
	--- not letting chop tree 
end

Edit: Probably should read this if you don’t know that childremoved does.

Hey, thank you for that, this works exactly as intended.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.