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

My image transparency won't change?

Here's the code:

local player = game.Players.LocalPlayer 
local char = player.Character or player.CharacterAdded:wait()
local humanoid = char.WaitForChild("Humanoid")
local health = humanoid.Health
while true do
	script.Parent.Parent.Parent.DYING.ImageTransparency = (health / 100)
	wait(.01)
end

It's essentially meant to have the image transparency of an imagelabel the same as the health of the person, and it's in a local script.

2 answers

✓ Accepted answer

Methods

First, on line 3 of your code, you're attempting to use the WaitForChild method on the char variable. Methods can only be called while using a colon : or, a period . while passing the subject as the first argument. A corrected version should look like this:

local player = game.Players.LocalPlayer 
local char = player.Character or player.CharacterAdded:wait()
local humanoid = char:WaitForChild("Humanoid") -- Changed to :

local health = humanoid.Health

while true do
    script.Parent.Parent.Parent.DYING.ImageTransparency = (health / 100)
    wait(.01)
end

Events

We don't always have to use events for everything, but it's best to utilize them as best as possible. In this case, there's a very specific event called HealthChanged that's a member of the Humanoid, which will update code whenever the health of the humanoid has been changed. We can use this event to replace your while loop, and this should be your revised code:

local player = game.Players.LocalPlayer 
local char = player.Character or player.CharacterAdded:wait()
local humanoid = char:WaitForChild("Humanoid") -- Changed to :

humanoid.HealthChanged:connect(function()
	local health = humanoid.Health -- let's also update this every time the health is changed, instead of keeping it at a static value.
	script.Parent.Parent.Parent.DYING.ImageTransparency = health / humanoid.MaxHealth -- let's divide it by the MaxHealth, so we can keep this relative.
end)

Hope that helped, let me know if you have any questions.

Just to clarify a bit,

Making a variable equal to a property, like on line 4, makes the variable equal to the property's current value.

It doesn't actually retain reference to the object itself, and therefore will not change if the property later changes.

Just make your variables equal to objects, then edit their properties.

local humanoid = char:WaitForChild("Humanoid")
. . .
humanoid.Health

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