Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

"Stamina" Is definitely there so why is it a nil value?

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.

2 comments

0

Player.Character:findFirstChild("StaminaValue") is returning nil, are you sure there is a value called that in the Character...? Nothing in this code has created it...

1

@DevSean It was created in another code

2 answers

✓ Accepted answer

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)

1 comment

0

Thanks man

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)

2 comments

0

Its neither the wrong name or the wrong location

0

@SH, are you putting the values in the Player or Character. Check the explorer when you play solo in studio,

This discussion is preserved as it was. New answers, comments, and votes are closed.