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

Hello How do i use a variable in another variable to adress something?

Asked by 5 years ago
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)

0
what are the things you are trying to define? terence404 19 — 5y

2 answers

Log in to vote
1
Answered by
oreoollie 649 Moderation Voter
5 years ago

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!

Ad
Log in to vote
-1
Answered by 5 years ago
Edited 5 years ago
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)
0
(-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. oreoollie 649 — 5y
0
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. terence404 19 — 5y
0
I don't appreciate the down vote by the way, I literally just joined scripting helpers. terence404 19 — 5y
0
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. Lugical 425 — 5y

Answer this question