Problem with "Attempt to index nil with "

Can i know what attempt to index with …something really mean? i’m quite confused

look, a TextLabel has a property called Text. nil doesn’t have a property called Text. when you write TextLabel.Text = tostring(FixedHealtVal), what you are really writing is nil.Text = tostring(FixedHealtVal), and since nil doesn’t have a Text property, and you are looking for the property ‘Text’ from nil, you are attempting to index nil with a property called Text that it doesn’t have, so it errors.

Unless you’re doing something outside this script to remove the script or something it should work just fine, I tried a new game with your exact setup. However, you could probably fix this by not making a local for the textlabel each frame. Simply move this part

local TextLabel = script.Parent

to outside the stepped loop like this

local TextLabel = script.Parent

RS.RenderStepped:Connect(function()

this way you have defined the textlabel when the script loads and even if the script is moved the textlabel variable will still point to the textlabel, no matter what.

“attempt to index nil with” simply means you’re trying to index something in nil
what you see

local variable = script:FindFirstChild("Object") -- Object doesnt exist so it returns nil
variable.Name = "hi"

but what is really going on is this

local variable = nil
nil.Name = "hi"

and since nil can’t be index, it errors

First, you have 3 useless statemetns, each statement gives the same reuslt, soo you can simply change it to:

local FixedHealthValue = 1

also what is important is that Health can’t be equal nil, which makes this statement usesless too

Second, your code is wrong, you placed constants under runService, which makes script not even bad, but lag machine! you don’t have to check if character is true, even if some strange weird error occur, local script is not required to work, as it’s not important.


-- Services related
local player = game:GetService("Players").LocalPlayer
local RunService = game:GetService("RunService")

-- Objects
local TextLabel = script.Parent
local Character = player.CharacteAdded:Wait() 

-- Change displayed health
local function OnRenderStepped()
    local Health = math.floor(Character.Humanoid.Health)
    TextLabel.Text = Health or "Player's health is 0"
end

RunService.RenderStepped:Connect(OnRenderStepped)

ExampleCode of what u try to achieve

EDIT: you really don’t have to change health every frame, you can use while loop running 4-5 times per second, players will see no difference, but game will be thankfull

-- Services related
local player = game:GetService("Players").LocalPlayer
local RunService = game:GetService("RunService")

-- Objects
local TextLabel = script.Parent
local Character = player.CharacteAdded:Wait() 

-- Change displayed health
local function UpdateHealth()
    local Health = math.floor(Character.Humanoid.Health)
    TextLabel.Text = Health or "Player's health is 0"
end

while true do
    UpdateHealth()
    task.wait(1/5)
end

Just a few changes I would make

Always try to index the character first, because if you do this when the character already loaded the script will yield until you respawn. Additionally if the gui resets on respawn it will only work if the character loads after the script.

Try avoid using loops, especially while loops as they can be exhausted and the script will break.
Instead I would use Humanoid.HealthChanged:Connect(UpdateHealth) as it only calls the update function when necessary

Oh i forget about events, still too much custom stuff when you have to use loops, also exhaustion don’t occur when there is wait statement, also local scripts can break and this is not that… important usually, but good point thx

I do believe loops can still be exhausted even if you have a wait, but I believe it only happens in case of a huge lag spike as roblox will break any loop

No they can’t be, exhaustion is caused by loop running too many times per second

while loops can be exhausted even if they have waits but it requires a very specific scenario
Watch exhausted | Streamable
In this clip I have one script the clears all children in workspace and another that will crash the game for a bit when clear all children is called, first I show it without the crash script enabled and then with. So what is need is a function that stalls the script for a longer period of time to be called in the while loop causing the loop to be exhausted.

Loop exhaustion still can be avoided by using them task.wait() instead of wait(), from what i know wait() can stop working randomly due to some lag, with task.wait() it’s like RunService

How would a child exist but parent wont? LMAO. Use your brain

Or just use HealthChanged? (9e9x better)

i have not seen the replies on this post. however, it seems that the code is good. i looked it up and i found this answer on someone’s post with a similar issue:

it seems u set “archivable” to true on the text label?

what do you mean, an instance doesn’t have to have a parent

Sorry for the trouble that the reply post i make 2nd is actually wrong error. I have pick a solution and the 2nd post reply made by me is actually a another script error that easily fixable. I overlook, Sorry again for the trouble

Error Actually:

Players.PowGeek.PlayerGui.Health.Health.Health:27: attempt to index nil with ‘Text’ - Studio

2nd post Reply error

Workspace.Powgeek.Health:11:Attempt to index nil with character

you do realize script needs to have a parent, script cannot exist if parent doesnt exist.
Its the same as u being born and have no father (father never existed)

1 Like

why does a script need to have a parent? if you write

script.Parent = nil

in middle of a script, it won’t stop

cause it already got executed???

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.