Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 9 years ago

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)

01local Player = game.Players.LocalPlayer
02 
03Player.CharacterAdded:connect(function  ()
04 
05Player = game.Players.LocalPlayer
06 
07 
08local Stamina = Player.Character:FindFirstChild("StaminaValue")
09local MaxStamina = Player.Character:FindFirstChild("MaxStaminaValue")
10 
11while wait() do
12    if Stamina.Value < MaxStamina.Value then
13        Stamina.Value = Stamina.Value + 1
14        wait(0.01)
15    end
16end
17end)

Please help.

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... DevSean 270 — 9y
1
@DevSean It was created in another code SHDrivingMeNuts 299 — 9y

2 answers

Log in to vote
1
Answered by
DevSean 270 Moderation Voter
9 years ago

Since you have said you are creating the values with some other code, you should change lines 8 and 9 from this:

1local Stamina = Character:FindFirstChild("StaminaValue")
2local MaxStamina = Character:FindFirstChild("MaxStaminaValue")

To the following

1local Stamina = Character:WaitForChild("StaminaValue")
2local 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.

01local player = game.Players.LocalPlayer
02 
03player.CharacterAdded:connect(function(newCharacter)
04    local stamina = newCharacter:WaitForChild("StaminaValue")
05    local maxStamina = newCharacter:WaitForChild("MaxStaminaValue")
06    while wait() do
07        if (stamina.Value < maxStamina.Value) then
08            wait() -- you had wait(0.01) here however the smallest wait time "wait()" is about 0.03 seconds
09            stamina.Value = stamina.Value + 1
10        end
11    end
12end)
0
Thanks man SHDrivingMeNuts 299 — 9y
Ad
Log in to vote
1
Answered by
legosweat 334 Moderation Voter
9 years ago
1local 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;

01local Player = game.Players.LocalPlayer
02local Character = Player.Character
03local Stamina = Character:FindFirstChild("StaminaValue")
04local MaxStamina = Character:FindFirstChild("MaxStaminaValue")
05 
06Player.CharacterAdded:connect(function()
07while wait() do
08    if Stamina.Value < MaxStamina.Value then
09        Stamina.Value = Stamina.Value + 1
10        wait(0.01)
11    end
12end
13end)
0
Its neither the wrong name or the wrong location SHDrivingMeNuts 299 — 9y
0
@SH, are you putting the values in the Player or Character. Check the explorer when you play solo in studio, legosweat 334 — 9y

Answer this question