I know that Stamina shouldnt be nil but it always gives this error:
18:54:57.446 - Players.Player.Backpack.StaminaRegen:14: attempt to index local 'Stamina' (a nil value)
local Player = game.Players.LocalPlayer Player.CharacterAdded:connect(function () Player = game.Players.LocalPlayer local Stamina = Player.Character:FindFirstChild("StaminaValue") local MaxStamina = Player.Character:FindFirstChild("MaxStaminaValue") while wait() do if Stamina.Value < MaxStamina.Value then Stamina.Value = Stamina.Value + 1 wait(0.01) end end end)
Please help.
Since you have said you are creating the values with some other code, you should change lines 8 and 9 from this:
local Stamina = Character:FindFirstChild("StaminaValue") local MaxStamina = Character:FindFirstChild("MaxStaminaValue")
To the following
local Stamina = Character:WaitForChild("StaminaValue") local MaxStamina = Character:WaitForChild("MaxStaminaValue")
As the values will be created after Stamina and MaxStamina have been initialised.
Here's your code rewritten a little bit.
local player = game.Players.LocalPlayer player.CharacterAdded:connect(function(newCharacter) local stamina = newCharacter:WaitForChild("StaminaValue") local maxStamina = newCharacter:WaitForChild("MaxStaminaValue") while wait() do if (stamina.Value < maxStamina.Value) then wait() -- you had wait(0.01) here however the smallest wait time "wait()" is about 0.03 seconds stamina.Value = stamina.Value + 1 end end end)
local Stamina = Player.Character:FindFirstChild("StaminaValue")
This variable may pass, but it can still be returned nil! For this to be nil, you are either getting "StaminaValue" from the wrong location, or you are addressing it by the wrong name.
Also, make sure the variable is a intValue, and not a boolean, string, etc. because you can't store numerical values in those.
EDIT: Let me rewrite your code;
local Player = game.Players.LocalPlayer local Character = Player.Character local Stamina = Character:FindFirstChild("StaminaValue") local MaxStamina = Character:FindFirstChild("MaxStaminaValue") Player.CharacterAdded:connect(function() while wait() do if Stamina.Value < MaxStamina.Value then Stamina.Value = Stamina.Value + 1 wait(0.01) end end end)