Ways to stop code when a player unequips a tool with a specific name?

I’m making a gathering system using proximity prompts, and I’ve run into several issues the community has assisted me with. Now the issue is, even though the animation cancels if a player unequips the hatchet, it essentially fast forwards the rewards. So, after the 5 second animation, it plays the TreeFalling sound, however, it also plays it if the player unequips the hatchet as well. I’ve tried some function calling, but it still seems to progress with the code even when using debounces. I’m completely stuck on this.

local prompt = script.Parent
local sound = script.Parent.Parent.ChoppingWood
local finishedsound = script.Parent.Parent.TreeFalling
local animation = game.ReplicatedStorage.Animations.Hatchet.swing2


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
		sound.TimePosition = 0
		sound.Playing = true
		loadAnimation:Play()
		
		local twait = 5
		local start = tick()
		repeat task.wait(0.1)
		until tick() - start > twait or not player.Character:FindFirstChild("Hatchet")
			loadAnimation:Stop()
			sound.TimePosition = 0
			sound.Playing = false
			finishedsound:Play()
			prompt.Enabled = true
			human.WalkSpeed = 18
		
	else
		player.PlayerGui.NoTool.NoHatchet.Visible = true
		
		
	wait(2)
		
		player.PlayerGui.NoTool.NoHatchet.Visible = false
	end
end)
1 Like

You need to keep track of your time outside of the function so that it is shared between triggers.

1 Like

Get the tool first, then use an event like .Unequipped. then make a boolean value from playing it when it gets unequipped. (I’ll explain more if u don’t get it after an hour I’m just workign on smth rn)