Script output not showing any errors but script not working?

Hello, guys i have been recently having problems with this script! There are no errors in the output!
Anything would help. Best would be solution thank you. Its a local script

Script:

game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
local player = game.Players.LocalPlayer
local char = player.Character
local hum = char:WaitForChild(“Humanoid”)
local ds = script.Parent.ImageLabel
local b = workspace.Sound
if hum.Health == 100 then
ds.Visible = false
b.Playing = false
elseif hum.Health <= 95 then
ds.visible = true
ds.ImageTransparency = 0.07
elseif hum.Health <= 90 then
ds.visible = true
ds.ImageTransparency = 0.13
elseif hum.Health <= 80 then
ds.visible = true
ds.ImageTransparency = 0.3
b.Playing = true
b.Volume = 0.1
elseif hum.Health <= 50 then
ds.visible = true
b.Volume = 0.3
ds.ImageTransparency = 0.5
elseif hum.Health <= 30 then
ds.visible = true
b.Volume = 0.5
ds.ImageTransparency = 0.7
elseif hum.Health <= 10 then
ds.visible = true
b.Volume = 1
ds.ImageTransparency = 0.8
hum.Died:Connect(function()
ds.visible = true
ds.ImageTransparency = 1
b.Volume = 0
b.Playing = false
end)
end
end)
end)

Image:
help1

2 Likes

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

1 Like

Another issue I noticed with your current code is some Property name errors. Remember Properties of an object will always be Capitalized,
In quite a few places, you have the Visibility property, .Visible, written as .visible, which will make the script search through the object for a Child named “visible” instead of changing the property.
Another error
Also, as it stands, should you really want to just use the elseif method, you should be checking if the number is More than or equal to the lower value, since you wrote the Health Values from top to bottom, meaning that the code will only ever see that the health value is less than 100, then that will be all it does, even if the code is also0 less than 90,80,70, etc… You should reverse the order of the checks, So do

if hum.Health<10 then
--code here
elseif hum.Health<=20 then
--code here
elseif hum.Health<=30 then
--etc...
1 Like