Hey there. Okay, so… that is quite a lot of code for what you are attempting to do. Now the issue with this script as it stands, is that it will run a single time, and will always ouput
ds.Visible = false, b.Playing = false, and that is it. Also, from a local GUI script, there is no need to get the game’s players, as this will run for every single person, looking for every single added person.
Unless the point of the GUI, is to show the health of every new player as they join, for each and every new player, meaning the GUI would change for every player that already exists, for whenever a new player joins the game. If you are testing this by yourself in studio, it would never show anything except your own health, whenever you join.
Beyond that, you are making a very common starting off error where you repeat the same line of code multiple times. This adds more for the game to process, and more for you to have to write
Now. First let’s talk about a general solution to your issue. If you want the GUI to change, based off of your own player’s health, you should use the .Changed Property of Health to do so, or even the specific HealthChanged Property of the humanoid.
Here’s a nice guide for the .Changed method and a nice resource for the Humanoid.HealthChanged Method
Now that you have that, you should remove this piece entirely from your script
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
and instead just make Player equal to game.Players.LocalPlayer
then you can attach the function to the humanoid of the player character.
local plr = game.Players.LocalPlayer
if plr then
local char = plr:WaitForChild("Character")
local hum = char:WaitForChild("Humanoid")
end
local ds = script.Parent.ImageLabel
local b = workspace.Sound
if hum.Health == 100 then
ds.Visible = false
b.Playing = false
end
of course, this is still really inefficient, as you are still using an if then statement for every single health.
So let’s go through it and see how we can take the constant changes,
ds.Visible = true/false, ds.ImageTransparency, b.Playing/Volume, etc…
and let us do one of two things.
Method One, we can take all the info we have for what the values should be for each level of health, and we can place them into a dictionary with that value as the key, then we can iterate (search through) the dictionary, and compare the values to each other, to see what the health of the humanoid is, then we can apply the values through a single function.
I don’t like this method as it relies on alot of info placement, and in the end is about as ridiculous as the above method.
Method 2 is setting an equation that equates the Transparency of the Image as a factor of the Health. You can just do ImageTransparency = (100 - hum.Health) /100