What is wrong about my charging script?

Hello, I’m trying to make a hammer that has to be charged before being able to be swung. It also has knock back, which it’s force is based on how long you’ve charged. The longer you charge, the stronger the knock back is. Tried using for loop but that doesn’t seem to work.

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 remoteEvent = tool.RemoteEvent

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
local charging = false

local force = 0

tool.Activated:Connect(function()
	if not cooldown and not hanging then
		cooldown = true
		
		if not charged and not charging then
			charging = true
			
			smashTrack:Stop()
			chargeTrack:Play()
			
			delay(chargeTrack.Length - 0.02999, function()
				chargeTrack:AdjustSpeed(0)
				
				for i = 0, 1000 do
					if charging then
						force = i
						wait()					
					end
				end
			end)
			
			charged = true
			
		end	
		wait(1)
		cooldown = false
		
	end
end)

tool.Deactivated:Connect(function()
	chargeTrack:Stop()
	
	if charged then
		charging = false

		smashTrack:Play()
		
		print(force) --I'm testing it first
		
		remoteEvent:FireServer(charged, force)
		
		delay(smashTrack.Length - 0.02999, function()
			smashTrack:AdjustSpeed(0)
			
			rootPart.Anchored = true
			hanging = true
			
			wait(1)
			smashTrack:Stop()
			
			rootPart.Anchored = false
			hanging = false
		end)
	end
	
	charged = false
end)

Please, bare with the long script. Thank you!!!

1 Like

inside the for loop do

if charging == false then
  force = i
  break
end

so if the tool is deactivated it breaks the loop and gives you the force number

1 Like
				for i = 0, 1000 do
					if not charging then
						force = i
						wait()
						break
					end
				end

is this what u mean sir?

1 Like

Yes, put the wait() outside the if statement but yes thats correct

if you remove the wait it would almost charge up instantly

				for i = 0, 1000 do
					wait()
					if not charging then
						force = i
						break
					end
				end
2 Likes