Maximum event re-entrancy depth exceeded for stamina script

i want to make stamina drain when its value isnt zero but it act weirdly and report Maximum event re-entrancy depth exceeded.


local stamina = script.Parent:FindFirstChild("Stamina")
local deb = false

if deb == false then
	deb = true
	stamina.Changed:Connect(function()
		stamina.Value = stamina.Value - 3
		wait(0.3)
	end)
if stamina.Value < 0 then
	stamina.Value = 0

end

if stamina.Value >= 100 then
	stamina.Value = 100
	end

end

1 Like

it’s because the function that you connected to .Changed is causing itself to fire

local RunService	= game:GetService("RunService")
local Stamina		= script.Parent:FindFirstChild("Stamina") :: NumberValue

local LossRate		= 0.9 -- lose 0.9 stamina every second

local function UpdateStamina(dt: number)
	local Value = Stamina.Value
	if Value <= 0 then
		Stamina.Value = 0
	elseif Value >= 100 then
		Stamina.Value = 100
	else
		Stamina.Value -= LossRate * dt
	end
end

RunService.Heartbeat:Connect(UpdateStamina)
2 Likes

Thank u so much about that,it helped me alot
wish u luck!
Dear Bush

By the way,can u tell me what is HeartBeat and what does it do in Lua script?this is the first time i saw this word in lua

Heartbeat is an Event that fires every frame that can be connected to different functions
It’s like a while true do loop but safer and more controllable

If your Roblox client is running at 60FPS, then Heartbeat will fire 60 times in 1 second
If your Roblox client is running at 120FPS, then Heartbeat will fire 120 times in 1 second

i understand it tyy!thanks alot for answering this

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