I'm making a hammer that has to be charged by .Activated before being able to be swung by .Deactivated. There is also a hang time after swinging that freezes the character. Please help, thanks much!

local tool = script.Parent

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local rootPart = character:WaitForChild("HumanoidRootPart")

local charge = script.Charge
local smash = script.Smash

local chargeTrack = animator:LoadAnimation(charge)
local smashTrack = animator:LoadAnimation(smash)

local cooldown = false
local charged = false
local hanging = false

tool.Activated:Connect(function()
	if not cooldown and not hanging then
		cooldown = true

		if not charged then
			smashTrack:Stop()
			chargeTrack:Play()
			
			delay(chargeTrack.Length - 0.02999, function()
				chargeTrack:AdjustSpeed(0)
			end)
			
			charged = true			
		end
		
		wait(1)
		cooldown = false
	end
end)

tool.Deactivated:Connect(function()
	chargeTrack:Stop()
	
	if charged then
		smashTrack:Play()
		
		delay(smashTrack.Length - 0.02999, function()
			smashTrack:AdjustSpeed(0)
			
			rootPart.Anchored = true
			hanging = true
			
			wait(1)
			smashTrack:Stop()
			
			rootPart.Anchored = false
			hanging = false
		end)
		
		charged = false
	end
end)

1 comment

0

It works but is always very buggy at the start of testings.