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
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 wait
in 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
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.
Change the background colour to red or any other colour you want. 4.Change the background transparency to 1.
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