local text = game.Workspace.Health_Teller.BillboardGui.health
game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:connect(function(character)
local player_name = player.Name
print(player_name)
local player_hum = game.Workspace.player.name.Humanoid
print(player_hum.Health)
end)
end)
Hello How do i use a variable in another variable to adress something?
2 answers
To answer your question, you need to wrap your variable in [ ]. Here is the new code.
local text = game.Workspace.Health_Teller.BillboardGui.health
game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:connect(function(character)
local player_name = player.Name
print(player_name)
local player_hum = game.Workspace[player_name].Humanoid
print(player_hum.Health)
end)
end)
But there is a better way to do this. The CharacterAdded event of Player passes a reference to the character (That's what the character argument in function(character) is). The code that utilizes the character argument is below.
local text = game.Workspace.Health_Teller.BillboardGui.health
game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:connect(function(character)
print(player.Name)
print(character:WaitForChild("Humanoid").Health)
end)
end)
I hope my answer could solve your problem! If it did please remember to mark it as correct!
local text = game.Workspace.Health_Teller.BillboardGui.health
game.Players.PlayerAdded:connect(function(player)
-- you dont need to add character added just say player.Character
local player_name = player.Name
print(player_name)
local player_hum = game.Workspace:WaitForChild(player_name).Humanoid -- fixed
print(player_hum.Health)
--removed one end because you can just say player.Character
end)
4 comments
(-1) The character added event is there for a reason, it's to make sure that the character actually exists before trying to read the health. This code errors because there is no character yet.
I put a WaitForChild so it works either way, in my scripting style I don't like using Remote Events or Events or functions in general for scripting.
I don't appreciate the down vote by the way, I literally just joined scripting helpers.
Might be way too late, but WaitForChild() is much more costly and takes up more memory in the game. The character added event would be the best choice out of whether to use it or not.
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
what are the things you are trying to define?