Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Value-Name thing for scripts?

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

✓ Accepted answer

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

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.