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

Value-Name thing for scripts?

Asked by
lucas4114 607 Moderation Voter
9 years ago

So I mean I set a Value for a script like this for example: health = 10 So then I want to make a humanoid name be updated with the health of the thing like this:

while wait(0.1) do
    script.Parent.Name = "Test Soilder ???????/10"
end

What I mean is that the "????????"/10 should show the health, for example if the health was 8 then it should be "Test Soilder 8/10" how would I do that?

2 answers

Log in to vote
1
Answered by 9 years ago

You will need to use concatenation here which is when you can add on information to an existing string. Concatenation is used with two dots, ... A script like the following would work.

while wait() do
    script.Parent.Name = "Test Soldier "..????????.."/10"
end
Ad
Log in to vote
1
Answered by
User#2 0
9 years ago

First and foremost, try using the Changed event instead of a while loop, it keeps from lagging the server. We'll want to use it on the Humanoid since that's where the Health is.

The rest is explained in the script, with comments, if you're confused.

-- Use WaitForChild so we can safely get the humanoid
-- Keep it as a variable since we'll use it a few times
local humanoid = script.Parent:WaitForChild("Humanoid")

-- Changed event provides one parameter: the name of the property that was changed (except with *Value instances)
humanoid.Changed:connect(function(property)
    -- Only update upon health changing
    if property == "Health" then
        -- Update the name accordingly
        -- Use `..` to concatenate a string and another object/string
        script.Parent.Name = "Test Soldier " .. humanoid.Health .. "/" .. humanoid.MaxHealth
    end
end)

Answer this question