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

What is wrong with this simple script?

I want this script to create a bloody screen as a player's health is dropping. The image will become denser and denser as the player's health becomes lower and lower. Can anyone solve this? I'm new to scripting and some of you may ignore this because it is so simple, it's just that I can't figure it out myself.

if script.Parent.Parent.Parent.Parent.Parent.Humanoid.Health <100 then
 script.Parent.ImageTransparency = 0.1
if script.Parent.Parent.Parent.Parent.Parent.Humanoid.Health <90 then
 script.Parent.ImageTransparency = 0.2
if script.Parent.Parent.Parent.Parent.Parent.Humanoid.Health <80 then
 script.Parent.ImageTransparency = 0.3
if script.Parent.Parent.Parent.Parent.Parent.Humanoid.Health <70 then
 script.Parent.ImageTransparency = 0.4
if script.Parent.Parent.Parent.Parent.Parent.Humanoid.Health <60 then
 script.Parent.ImageTransparency = 0.5
if script.Parent.Parent.Parent.Parent.Parent.Humanoid.Health <50 then
 script.Parent.ImageTransparency = 0.6
if script.Parent.Parent.Parent.Parent.Parent.Humanoid.Health <40 then
 script.Parent.ImageTransparency = 0.7
if script.Parent.Parent.Parent.Parent.Parent.Humanoid.Health <30 then
 script.Parent.ImageTransparency = 0.8
if script.Parent.Parent.Parent.Parent.Parent.Humanoid.Health <20 then
 script.Parent.ImageTransparency = 0.9
if script.Parent.Parent.Parent.Parent.Parent.Humanoid.Health <10 then
 script.Parent.ImageTransparency = 1
end
end
end
end
end
end
end
end
end
end

3 answers

✓ Accepted answer

Here's a script much simplier than anything you did..

--//Code
--This must be in a localScript!
script.Parent.ImageTransparency  =  script.Parent.Parent.Parent.Parent.Parent.Humanoid.Health/100
--//

1 comment

0

Forgot to put an explaintion: First. What it does is that find your hp and divide by 100 so if you have fullHp your image transparency will be 1(Invisible) else if you have like 50 hp then the image will have a transparency of 0.5 wich is semi invisible, i don't think it's hard to understand what i coded. Have a good day!

Well First you gotta make sure its a localscript inside whatever you're trying to make become transparent

your code was very long and not efficient using elseif instead of if clears up all those ends

setting a variable using local variablename = game.location.objectname so you don't have all those parents

you neede to locate the players character therefore it was breaking because it didn't know where the character was so player.Character is what you have to do after setting the players variable

-- alright first lets locate the plater
local player = game.Players.LocalPlayer
-- now lets locate the frame
local guiThing = script.Parent -- makes the code shorter so you don't type as much

if player.Character.Humanoid.Health <=100 then
 guiThing.ImageTransparency = 1
elseif player.Character.Humanoid.Health <=90 then -- using elseif cleans up the code so you don't have 10 ends
 guiThing.ImageTransparency = .9
elseif player.Character.Humanoid.Health <=80 then
 guiThing.ImageTransparency = .8
elseif player.Character.Humanoid.Health <=70 then
 guiThing.ImageTransparency = .7
elseif player.Character.Humanoid.Health <=60 then
 guiThing.ImageTransparency = .6
elseif player.Character.Humanoid.Health <=50 then
 guiThing.ImageTransparency = .5
elseif player.Character.Humanoid.Health <=40 then
 guiThing.ImageTransparency = .4
elseif player.Character.Humanoid.Health <=30 then
 guiThing.ImageTransparency = .3
elseif player.Character.Humanoid.Health <=20 then
 guiThing.ImageTransparency = .2
elseif player.Character.Humanoid.Health <=10 then
 guiThing.ImageTransparency = .1
end

Hope this works and help and have a wonderful day! :)

You are missing the =. When you are using an if,while or repeat statement to check if a value is equal, higher or lower than another, you must add an extra = along with the other =, <, or >. That way the script differentiates:

if variable == value then variable = value end --Variable equal to value
if variable <= value then variable = value end --Variable less than value
if variable >= value then variable = value end --Varable more than value

Like you did at your script, when you want to change the value of a property, you use just one =. Here's your corrected script, replacing < with <=:

if script.Parent.Parent.Parent.Parent.Parent.Humanoid.Health <= 100 then
 script.Parent.ImageTransparency = 0.1
if script.Parent.Parent.Parent.Parent.Parent.Humanoid.Health <= 90 then
 script.Parent.ImageTransparency = 0.2
if script.Parent.Parent.Parent.Parent.Parent.Humanoid.Health <= 80 then
 script.Parent.ImageTransparency = 0.3
if script.Parent.Parent.Parent.Parent.Parent.Humanoid.Health <= 70 then
 script.Parent.ImageTransparency = 0.4
if script.Parent.Parent.Parent.Parent.Parent.Humanoid.Health <= 60 then
 script.Parent.ImageTransparency = 0.5
if script.Parent.Parent.Parent.Parent.Parent.Humanoid.Health <= 50 then
 script.Parent.ImageTransparency = 0.6
if script.Parent.Parent.Parent.Parent.Parent.Humanoid.Health <= 40 then
 script.Parent.ImageTransparency = 0.7
if script.Parent.Parent.Parent.Parent.Parent.Humanoid.Health <= 30 then
 script.Parent.ImageTransparency = 0.8
if script.Parent.Parent.Parent.Parent.Parent.Humanoid.Health <= 20 then
 script.Parent.ImageTransparency = 0.9
if script.Parent.Parent.Parent.Parent.Parent.Humanoid.Health <= 10 then
 script.Parent.ImageTransparency = 1
end
end
end
end
end
end
end
end
end
end

This should work.

5 comments

0

Your code is still very sloppy and doesn't locate the character to determine the humanoids health therefore their's still an error...

1

Hm, i forgot. You're right. But please, if you are the one who disliked, revert it.

0

I wasn't it was probably the question maker

0

@Rathdogg1765 Then how do I fix that, none of these scripts have seemed to work.

0

Wasn't me who disliked it.

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