How can we optimize my hammer script?

My hammer has to be charged before being able to be swung. Upon swinging, the local character would freeze and anchored for a bit. It also has knock back which bounces characters back in sort of an arc form. The longer the hammer is charged, the stronger the force is. Help is HIGHLY APPRECIATED! I have OCD and the mess of my script is stressing me out. Thanks! <3

To further understand what I mean by my hammer, kindly visit this place:
https://www.roblox.com/games/6309845499/Hammer-Demo

Client:

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 magnitude = 100

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)
			end)
			charged = true
			
			for i = 50, 100 do
				if charging then
					wait()
					magnitude = i
					print(magnitude)
				end
			end
			
		end	
		wait(1)
		cooldown = false
	end
end)

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

		smashTrack:Play()
				
		remoteEvent:FireServer(character, charged, magnitude)
		
		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)

Server:

local tool = script.Parent
local head = tool.Handle

local remoteEvent = tool.RemoteEvent

local smash

remoteEvent.OnServerEvent:Connect(function(player, character, charged, magnitude)
	local rootPart = character:WaitForChild("HumanoidRootPart")
	
	if charged then
		print(magnitude)
		
		smash = head.Touched:Connect(function(hit)
			local humanoid = hit.Parent:FindFirstChild("Humanoid")
			local hitCharacter = hit.Parent
			local hitRootPart = hitCharacter:WaitForChild("HumanoidRootPart")
			
			if humanoid then
				local knockback = Instance.new("BodyVelocity")
				
				knockback.Parent = hitRootPart
				knockback.Velocity = (rootPart.CFrame.LookVector * magnitude) + (rootPart.CFrame.UpVector * magnitude)
				knockback.MaxForce = knockback.MaxForce * magnitude
				wait()
				knockback:Destroy()
			end
		end)
		wait(0.3)
		smash:Disconnect()
	end
end)

I think both scripts are well optimized, I don’t see anything out of the ordinary

1 Like

thanks for letting me know ;-;