Stamina keep increasing when the attack event is fired

Hi, I’m actually working on a stamina system. The thing is, I want the increasing loop to break when the SetstaminaEvent is fired, but it is not working.

Here’s the code:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local UserInputService = game:GetService("UserInputService")

local Values = Character:WaitForChild("Values")
local Stamina = Values.Stamina
local Running = false
local SprintSpeed = 30
local WalkSpeed = 16
local Setstamina = game:GetService("ReplicatedStorage").Events.StaminaChanged

-- Add a new value to keep track of whether or not the resistance value is currently increasing
local IncreasingResistance = false

local function updateStaminaBar()
	script.Parent.StaminaBar.Bar:TweenSize(UDim2.new(Stamina.Value / 100, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.1)
end

local function increaseStamina()
	script.Parent.StaminaBar.IncreasingStamina.Value = true

	for i = Stamina.Value, 100 do
		if IncreasingResistance then
			warn("Breaking out of loop because IncreasingResistance is", IncreasingResistance)
			break
		end

		Stamina.Value = i
		print("Stamina.Value set to", Stamina.Value)
		task.wait()
	end

	if Stamina.Value == 100 then
		script.Parent.StaminaBar.IncreasingStamina.Value = false
	end
end



function SetstaminaEvent(newStaminaValue)
	Stamina.Value = newStaminaValue

	-- Add a delay before calling increaseStamina
	task.delay(2, function()
		increaseStamina()
	end)

	-- Set IncreasingResistance to false
	IncreasingResistance = false
end



Setstamina.Event:Connect(function(value)
	IncreasingResistance = true
	SetstaminaEvent(value)
end)


task.spawn(function()
	while true do
		updateStaminaBar()
		task.wait()
	end
end)

Character.Humanoid:GetPropertyChangedSignal("Health"):connect(function()
	script.Parent.HealthBar.Bar.Size = UDim2.new(math.floor(Character.Humanoid.Health) / 100, 0, 1, 0)
end)

Here’s a video of the problem:

Hello, i think this might work for you. Please let me know if it works for you!

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local UserInputService = game:GetService("UserInputService")

local Values = Character:WaitForChild("Values")
local Stamina = Values.Stamina
local Running = false
local SprintSpeed = 30
local WalkSpeed = 16
local Setstamina = game:GetService("ReplicatedStorage").Events.StaminaChanged
local breaktheloop = false

-- Add a new value to keep track of whether or not the resistance value is currently increasing
local IncreasingResistance = false

local function updateStaminaBar()
	script.Parent.StaminaBar.Bar:TweenSize(UDim2.new(Stamina.Value / 100, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.1)
end

local function increaseStamina()
	script.Parent.StaminaBar.IncreasingStamina.Value = true

	for i = Stamina.Value, 100 do
		if breaktheloop == false then
			if IncreasingResistance then
				warn("Breaking out of loop because IncreasingResistance is", IncreasingResistance)
				break
			end

			Stamina.Value = i
			print("Stamina.Value set to", Stamina.Value)
			task.wait()
		end
	end

	if Stamina.Value == 100 then
		script.Parent.StaminaBar.IncreasingStamina.Value = false
	end
end



function SetstaminaEvent(newStaminaValue)
	breaktheloop = true
	Stamina.Value = newStaminaValue

	-- Add a delay before calling increaseStamina
	task.delay(2, function()
		increaseStamina()
	end)

	-- Set IncreasingResistance to false
	IncreasingResistance = false
	breaktheloop = false
end



Setstamina.Event:Connect(function(value)
	IncreasingResistance = true
	SetstaminaEvent(value)
end)


task.spawn(function()
	while true do
		updateStaminaBar()
		task.wait()
	end
end)

Character.Humanoid:GetPropertyChangedSignal("Health"):connect(function()
	script.Parent.HealthBar.Bar.Size = UDim2.new(math.floor(Character.Humanoid.Health) / 100, 0, 1, 0)
end)
1 Like

Ty for the response, but it’s not working. I think is because the issue is with the loop condition in the increaseStamina function, I’ll be testing what is going on there.

So how exactly are you detecting if a player is hitting or using something that requires stamina. I don’t see any Revents.

After reviewing the code I have realized you were using a Delay while setting the new stamina. This may cause further requests to be overwritten.

function SetstaminaEvent(newStaminaValue)
	Stamina.Value = newStaminaValue

	-- Add a delay before calling increaseStamina
	task.delay(2, function()
		increaseStamina()
	end)

	-- Set IncreasingResistance to false
	IncreasingResistance = false
end

This is what is causing the problem. You can actually put it in a task.spawn function to avoid yielding (if that is what the delay was for)

function SetstaminaEvent(newStaminaValue)
    task.spawn(function()
	Stamina.Value = newStaminaValue
	increaseStamina()
	IncreasingResistance = false
    end)
end
1 Like

And will it work when we put the loop in a repeat until checkpoint?

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local UserInputService = game:GetService("UserInputService")

local Values = Character:WaitForChild("Values")
local Stamina = Values.Stamina
local Running = false
local SprintSpeed = 30
local WalkSpeed = 16
local Setstamina = game:GetService("ReplicatedStorage").Events.StaminaChanged
local breaktheloop = false

-- Add a new value to keep track of whether or not the resistance value is currently increasing
local IncreasingResistance = false

local function updateStaminaBar()
	script.Parent.StaminaBar.Bar:TweenSize(UDim2.new(Stamina.Value / 100, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.1)
end

local function increaseStamina()
	script.Parent.StaminaBar.IncreasingStamina.Value = true

	repeat
		for i = Stamina.Value, 100 do
			if IncreasingResistance then
				warn("Breaking out of loop because IncreasingResistance is", IncreasingResistance)
				break
			end

			Stamina.Value = i
			print("Stamina.Value set to", Stamina.Value)
			task.wait()
		end
	until breaktheloop == true

	if Stamina.Value == 100 then
		script.Parent.StaminaBar.IncreasingStamina.Value = false
	end
end



function SetstaminaEvent(newStaminaValue)
	breaktheloop = true
	Stamina.Value = newStaminaValue

	-- Add a delay before calling increaseStamina
	task.delay(2, function()
		increaseStamina()
	end)

	-- Set IncreasingResistance to false
	IncreasingResistance = false
	breaktheloop = false
end



Setstamina.Event:Connect(function(value)
	IncreasingResistance = true
	SetstaminaEvent(value)
end)


task.spawn(function()
	while true do
		updateStaminaBar()
		task.wait()
	end
end)

Character.Humanoid:GetPropertyChangedSignal("Health"):connect(function()
	script.Parent.HealthBar.Bar.Size = UDim2.new(math.floor(Character.Humanoid.Health) / 100, 0, 1, 0)
end)
1 Like

the code you sent crashed my roblox studio, give me a moment.

Alr, i’ll test in a moment. Just get me 5 minutes.

the code causes the stamina value to increase infinitely

image

Try my code out. It should be the original fix to the issue.

1 Like

I got a solution, the IncreasingResistance variable is being set to false in the SetstaminaEvent function before the increaseStamina function is called.

this is the part of the code that fix the bug:

function SetstaminaEvent(newStaminaValue)
	Stamina.Value = newStaminaValue

	-- Add a delay before calling increaseStamina
	task.delay(2, function()
		increaseStamina()
		IncreasingResistance = false
	end)
end

This is the code now:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local UserInputService = game:GetService("UserInputService")

local Values = Character:WaitForChild("Values")
local Stamina = Values.Stamina
local Running = false
local SprintSpeed = 30
local WalkSpeed = 16
local Setstamina = game:GetService("ReplicatedStorage").Events.StaminaChanged

-- Add a new value to keep track of whether or not the resistance value is currently increasing
local IncreasingResistance = false

local function updateStaminaBar()
	script.Parent.StaminaBar.Bar:TweenSize(UDim2.new(Stamina.Value / 100, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.1)
end

local function increaseStamina()
	script.Parent.StaminaBar.IncreasingStamina.Value = true

	for i = Stamina.Value, 100 do
		if IncreasingResistance then
			warn("Breaking out of loop because IncreasingResistance is", IncreasingResistance)
			break
		end

		Stamina.Value = i
		print("Stamina.Value set to", Stamina.Value)
		task.wait()
	end

	if Stamina.Value == 100 then
		script.Parent.StaminaBar.IncreasingStamina.Value = false
	end
end


function SetstaminaEvent(newStaminaValue)
	Stamina.Value = newStaminaValue

	-- Add a delay before calling increaseStamina
	task.delay(2, function()
		increaseStamina()
		IncreasingResistance = false
	end)
end




Setstamina.Event:Connect(function(value)
	IncreasingResistance = true
	SetstaminaEvent(value)
end)


task.spawn(function()
	while true do
		updateStaminaBar()
		task.wait()
	end
end)

Character.Humanoid:GetPropertyChangedSignal("Health"):connect(function()
	script.Parent.HealthBar.Bar.Size = UDim2.new(math.floor(Character.Humanoid.Health) / 100, 0, 1, 0)
end)

feel free to copy it if you want.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.