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

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.

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

2 answers

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

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)
0
Thanks man SHDrivingMeNuts 299 — 8y
Ad
Log in to vote
1
Answered by
legosweat 334 Moderation Voter
8 years ago
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)
0
Its neither the wrong name or the wrong location SHDrivingMeNuts 299 — 8y
0
@SH, are you putting the values in the Player or Character. Check the explorer when you play solo in studio, legosweat 334 — 8y

Answer this question