Help with tools activated and touched

local cooldown = 1.1 - (axe.Value/10)
local swinging = false

tool.Activated:Connect(function()
if swinging then return end
	swinging = true
	
	local playerGui = player:FindFirstChild('PlayerGui')
	if playerGui then
		local cooldownHUDClone = cooldownHUD:Clone()
		cooldownHUDClone.Parent = playerGui
			
		for i = cooldown, 0, -0.1 do
			cooldownHUDClone.Backing.Bar.Size = UDim2.new(i/1, 0, 1, 0)
			wait(0.1)
		end
						
		cooldownHUDClone:Destroy()
		swinging = false
		if marketplaceService:UserOwnsGamePassAsync(player.UserId, 5520787) then 
			humanoid.WalkSpeed = 32
		elseif marketplaceService:UserOwnsGamePassAsync(player.UserId, 5520830) then 
			humanoid.WalkSpeed = 24
		else 
			humanoid.WalkSpeed = 16
		end
		humanoid.JumpPower = 50
	end
end)

handle.Touched:Connect(function(hit)
if swinging then
		if hit then
			if hit.Name == 'Trunk' then
				local strength = hit.Parent:FindFirstChild('Strength')
				if strength then
					
					humanoid.WalkSpeed = 0
					humanoid.JumpPower = 0
					
					local loadSlash = humanoid:LoadAnimation(slash)
					
					if loadSlash then
						loadSlash:Play()
					end
									
					strength.Value = strength.Value - (0.5+(axe.Value*0.5))
					if strength.Value == 0 then
						cooldownHUD.Backing.Health.Text = '5 / 5'
					else
						cooldownHUD.Backing.Health.Text = strength.Value .. ' / 5'
					end
					
					if strength.Value <= 0 then
						local treeClone = hit.Parent:Clone()
						hit.Parent:Destroy()
						resourceCollected:FireServer('Wood')
						spawn(function()
							wait(60)
							treeClone.Parent = workspace
							strength.Value = 5
						end)
					end
				end	
			end
		end
end
end)

This is just the main part to my tools script. It brings up a UI when the tool is activated (a cooldown bar UI) as well as the items ‘health’ (strength is how it’s referenced) I want to make this UI pop up only when the handle is actually cutting down a tree, as you can see in the touched event, hit.Name == 'Trunk' I want it to go after that, so it only shows up when you are actually cutting down a tree, not every time you’ve clicked. I’ve run into a ton of problems however moving it down there. Trees being cut down in one click, being able to spam click instead of having that cooldown. That’s another problem. When a player spam clicks, even with this current script, they can cut a tree down in 1-3 clicks, instead of 5. It only takes away 1 damage per, strength.Value = strength.Value - (0.5+(axe.Value*0.5)) Which is only taking away 1, so it should be 5, 4, 3, 2, 1 then 0 would destroy the tree. But it sometimes goes like 5 - 3 - 0, or 5 - 1 - 0 or even sometimes 5 - 0. Any help would be appreciated

1 Like

To fix the strength issue, couldn’t you do
strength.Value = math.clamp(strength.Value-1,0,5)
This will prevent returning a value lower than 0, and decreases it by one each time.

For the UI, is everything enabled/visible?

Everything in the UI is enabled. It works well as given, but it just works when activated instead of on touched, but when i tried putting it inside the touched event it bugs up.

Bugs up how?

EDIT:
I see, you need to have the cooldown between each touch instead of activation of the tool, because you’re destroying it shortly after. Destroy it once the health is gone and the tree is chopped.