Whenever I try to use this, it attempts to index "character" but it is a nil value, what would I use to solve this?
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
print(player.Character.Humanoid.Health)
Whenever I try to use this, it attempts to index "character" but it is a nil value, what would I use to solve this?
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
print(player.Character.Humanoid.Health)
The character is not loaded instantly when a player joins. Your code is executing too soon.
Execute this code in a CharacterAdded event listener.
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
print(character:WaitForChild("Humanoid").Health)
end)
end)
However it's also possible that the reverse can also occur. Your event listener could have been established too late so you'd have to wait for the character to respawn. Just before establishing your event listener you should check if the character first exists.
Players.PlayerAdded:Connect(function(player)
if player.Character then
print(player.Character:WaitForChild("Humanoid").Health)
end
player.CharacterAdded:Connect(function(character)
print(character:WaitForChild("Humanoid").Health)
end)
end)
Thank you so much
You're likely trying to print it before the character exists. If we use CharacterAdded we can be sure the character exists before printing.
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
print(character.Humanoid.Health)
end)
end)
how would I accept the answer? Thank you so much
It says it on the right side of the answer
oof low rep people can't accept answers
Are you sure about that?
I was able to finally accept it thank you!! =)
This discussion is preserved as it was. New answers, comments, and votes are closed.
9 comments
By the way you should be able to accept my answer which should be under the answer itself, or even hovering over the answer might prompt you to accept.
https://gyazo.com/d9fa69b685177b4b260a3d9a3636d810
I wonder if because I have -5 reputation I can't accept it, but I am not seeing it for some reason, but I will check thank you
Try it now.
It must be my browser or something, I'm not sure :( https://imgur.com/a/0M2WuQT is a picture of the screen, I still am not seeing it for some reason
Try hovering over the answer see if a little prompt that tells you to accept it. Otherwise I do not know
My account is pretty new, could that be it? and is there a certain part of the answer or just the box that the answer is contained in?
As far as im aware there are no restrictions to being able to accept answers.
I am so sorry I will try to figure this out :/ I might need to get help from support for this.