Movement with Stamina & Weapon System; Unable to use weapon again after reaching a certain stamina threshold? - Customizing SwordSmith

I am making a weapon and movement system that uses stamina. After reaching a certain stamina threshold, it becomes impossible to use a weapon again. Despite stamina regeneration, the player is unable to initiate weapon swings. For weapons I’m using SwordSmith. I tried to use ChatGPT to fix it and I searched DevForum to find solution.

1. One of the weapons Script (SwordSmith with modifications):

local ServerStorage = game:GetService("ServerStorage")
local replicatedStorage = game:GetService("ReplicatedStorage")
local SwordSmith = require(ServerStorage:WaitForChild("SwordSmith"))

local tool = script.Parent
local debounce = false

local data = {
	--blah blah, weapon settings
}

local sword = SwordSmith.new(tool, data)
local class = tool.class

local cooldown = .15
local damage = 20
local canhit = true

replicatedStorage.Remotes.toolSwing2.OnServerEvent:Connect(function(x, power, heavy)
	if tonumber(power) <= heavy then
		canhit = false
		print("No stamina, can't hit, "..tonumber(power))
	elseif tonumber(power) > heavy then
		canhit = true
		print("Enough stamina to hit, "..tonumber(power))
	end
end)

local Swing = tool.Handle.Swing
local Hit = tool.Handle.Hit
local Equip = tool.Handle.Equip

tool.Activated:Connect(function()
	local hum = tool.Parent:WaitForChild("Humanoid")
	
	if hum.Health >= 1 and canhit and not debounce then
		debounce = true
		
		sword:Swing(damage)
		replicatedStorage.Remotes.toolSwing:FireClient(game:GetService("Players"):GetPlayerFromCharacter(tool.Parent), class, canhit)
		Swing:Play()
		
		print(tool.Name..", "..damage)
	end
end)

tool.Equipped:Connect(function()
	Equip:Play()
end)

sword:OnSwingEnded(function()
	wait(cooldown)
	debounce = false
end)

sword:OnHumanoidHit(function()
	Hit:Play()
end)

2. Movement/Stamina script:

--library etc.

local starterpower
local power --otherwise stamina

local function GetStamina()
	if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(client.UserId, 193096403) then
		return Settings.gamepass_stamina
	else
		return Settings.default_stamina
	end
end

power = GetStamina()
starterpower = power

local function RegeneratePower()
	while power < starterpower and not sprinting and not crouching and not usingWeapon do
		power += Settings.power_regeneration_rate
		bar.Size = UDim2.new(power / GetStamina(), 0, 1, 0) --UI stamina bar update
		wait(.05)
	end
end

--rest od code, movement  system etc.

repeat wait() until game:IsLoaded() and character

replicatedStorage.Remotes.toolSwing.OnClientEvent:Connect(function(class, canhit)
	local light = 10
	local medium = 20
	local heavy = 30

	if class.Value == "light" and power >= light and canhit then --check class
		power -= light
	elseif class.Value == "medium" and power >= medium and canhit then
		power -= medium
	elseif class.Value == "heavy" and power >= heavy and canhit then
		power -= heavy
	end

	replicatedStorage.Remotes.toolSwing2:FireServer(power, light, medium, heavy) --send info to weapon

	bar.Size = UDim2.new(power / GetStamina(), 0, 1, 0) --UI stamina bar update
end)

while task.wait() do
	RegeneratePower()
end

I suppose that I should find a way to check when ‘power’ is sufficient, in this case more or equal than 30? I’m kinda lost. Ngl I think there might be a better and less complicated way to do it. :sob: