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

ImageLabel script help?

I want to make a call of duty death screen so its like when you are injured/ losing health, the screen starts to turn red. The screen either slowly goes away if you are not being injured or the screen will be bloody red when you are dead. I tried to make a script that would work like that but its not working and Idk if im right or not. This script is under an image label btw.


Player = script.Parent.Parent.Parent.Parent.Character
s = Player.Humanoid
while true do
if s.Health <= 90 then
script.Parent.ImageTransparency = .1
script.Parent.Visible = true
while true do
if s.Health <= 80 then
script.Parent.ImageTransparency = .2
script.Parent.Visible = true
while true do
if s.Health <= 70 then
script.Parent.ImageTransparency = .3
script.Parent.Visible = true
while true do
if s.Health <= 60 then
script.Parent.ImageTransparency = .4
script.Parent.Visible = true
if s.Health >= 50 then
script.Parent.ImageTransparency = .5
script.Parent.Visible = true
while true do
if s.Health <= 40 then
script.Parent.ImageTransparency = .6
script.Parent.Visible = true
while true do
if s.Health <= 30 then
script.Parent.ImageTransparency = .7
script.Parent.Visible = true
while true do
if s.Health <= 20 then
script.Parent.ImageTransparency = .8
script.Parent.Visible = true
while true do
if s.Health <= 10 then
script.Parent.ImageTransparency = .9
script.Parent.Visible = true
while true do
if s.Health <= 0 then
script.Parent.ImageTransparency = 0
script.Parent.Visible = true
end
end

2 answers

✓ Accepted answer

Ok...so you made a couple mistakes. I'll try to explain them after this, but try this new script:

Player = script.Parent.Parent.Parent.Parent.Character
s = Player.Humanoid
while true do
	wait() --Needed so that it doesn't crash the game.
		script.Parent.ImageTransparency = (s.Health/100) --Credit to BlueTaslem for suggesting this
	end
end

So basically, I removed all the "while true do" statements. These wouldn't have worked anyways because you need to put a waitin their, so that it doesn't run infinitely and crash the game.

Also, I removed all of the if statements, and replaced them with a single line, that changes the transparency based on the players health.

Anyways, try using this script, and it should work now. If you have any further questions, please leave a comment below, and i'll see what I can do. Hope I helped :P

4 comments

2

why even use ifs when the formula for Transparency in terms of Health is so simple?

0

Updated. Thanks for pointing that out, lol.

0

Thank you so much!! The both of you! You guys are life savers!

0

Haha, no prob. Glad I could help.

You have the right idea for the logic but you should using loop like that as they are all infinite. Instead you should use this event.

HealthChanged note this is a localscript

Use this event to change the image transparency and remove all of the while loops.

Edit:-

Ok I have made a small piece of code which will help you.

  1. First you will need to add a screengui into the startergui, inside this place a frame.

  2. Change the frames size to this "{1, 0},{1, 0}" this will fill up the whole screen.

  3. Change the background colour to red or any other colour you want. 4.Change the background transparency to 1.

  4. add this localscript into the starterpack


local humanoid = game.Players.LocalPlayer.Character.Humanoid --Finds the humanoid of the local character. This is the player who is running the script.

local gui = game.Players.LocalPlayer.PlayerGui --the players gui

local gui1 = gui:WaitForChild("ScreenGui") --waits till the gui is available

humanoid.HealthChanged:connect(function(health)
	--sets the transparency of the background to the players health divided by 100 as that is the players max health
	gui1.Frame.BackgroundTransparency = health /100
end)

Hope this helps

1 comment

0

Can u elaborate more on that? Im still a novice scripter... and im stll confused on what you mean by changing the image to helath change...

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