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
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)
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