I'm trying to make a gui that changes color along with a Humanoid (in workspace) health. But all that happens is it turns black (the part of the gui I'm working with) and doesnt do anything. Any help is appreciated :D
while true do MyGuy = script.Parent.Parent.Header.Text Note = game.Workspace:FindFirstChild(""..MyGuy) Health = Note.Humanoid.Health MaxHealth = Note.Humanoid.MaxHealth All = Health / MaxHealth Color = script.Parent.BackgroundColor3 if All >= .8 then Color = Color3.new(0,170,0) if All >= .6 and All < .8 then Color = Color3.new(102, 170, 12) if All >= .4 and All < .6 then Color = Color3.new(255, 255, 0) if All >= .2 and All < .4 then Color = Color3.new(255, 170, 0) if All > 0 and All < .2 then Color = Color3.new(255, 0, 0) if All == 0 then Color = Color3.new(255,255,255) script.Parent.Text = ""..script.Parent.Text.."(Dead)" end end end end end end end
This is what it has come to
while true do wait (1) MyGuy = script.Parent.Text Note = game.Workspace:WaitForChild(MyGuy) Humanoid = Note:WaitForChild("Humanoid") Color = script.Parent if (Humanoid.Health / Humanoid.MaxHealth) >= .8 then Color.BackgroundColor3 = Color3.new(0,170,0) if (Humanoid.Health / Humanoid.MaxHealth) >= .6 and (Humanoid.Health / Humanoid.MaxHealth) < .8 then Color.BackgroundColor3 = Color3.new(102/255, 170/255, 12/255) if (Humanoid.Health / Humanoid.MaxHealth) >= .4 and (Humanoid.Health / Humanoid.MaxHealth) < .6 then Color.BackgroundColor3 = Color3.new(255/255, 255/255, 0) if (Humanoid.Health / Humanoid.MaxHealth) >= .2 and (Humanoid.Health / Humanoid.MaxHealth) < .4 then Color.BackgroundColor3 = Color3.new(255/255, 170/255, 0) if (Humanoid.Health / Humanoid.MaxHealth) > 0 and (Humanoid.Health / Humanoid.MaxHealth) < .2 then Color.BackgroundColor3 = Color3.new(255/255, 0, 0) if (Humanoid.Health / Humanoid.MaxHealth) == 0 then Color.BackgroundColor3 = Color3.new(255/255,255/255,255/255) script.Parent.Text = script.Parent.Text.."(Dead)" end end end end end end end
No clue why, but it still is not working =/
3 Things that would cause this script to error:
1) You have no wait() functions in your script. The while loop will just instantly crash itself and break. Add a bit of wait time, such as wait(.1) or something.
2) On line 7, you are assigning "Color" to the actual property's value, not the property. You aren't actually changing anything inside the textbutton. You have to set your variable to the actual instance, and then go from the instance to the property every time (It's tedious, but it is what it is).
3) Color3 values in ROBLOX do not go from 0 - 255; instead they go from 0-1. To get what you're looking for, Color3.new(0, 50/255, 0)
would produce a 0, 50, 0 RGB value. Dividing the number by 255 should get the same value when the proportion is equal to 1.
FINAL EDIT:
"The script STILL doesn't work!!!!!"
Finally discovered the problem. Here is what is happening: If you keep them all as "if" statements, then they only run if the previous statement is true (provided that you keep all your if statements in one scope, which you did). When you change your health, it does NOT return true, thus not running any other if statements and it skips to the end of the loop to restart. To fix this, you use elseif, which is what I used below. It does not need an end, since it does not create a new scope. The script now works, I tested it in my own studio. If it errors for you, your hierarchy is incorrect. Make sure that this script is in a Script (NOT a LocalScript) because LocalScripts cannot access Workspace. Finally fixed. :D
I also found it easier to move the variables outside of the loop to save run time, since it won't waste time assigning variables with every loop it makes.
With all of these in mind, this would be the script (Assuming your hierarchy is correct):
MyGuy = script.Parent.Parent.Header.Text Note = game.Workspace:WaitForChild(MyGuy) Humanoid = Note:WaitForChild("Humanoid") Color = script.Parent while true do if (Humanoid.Health / Humanoid.MaxHealth) >= .8 then Color.BackgroundColor3 = Color3.new(0,170/255,0) elseif (Humanoid.Health / Humanoid.MaxHealth) >= .6 and (Humanoid.Health / Humanoid.MaxHealth) < .8 then Color.BackgroundColor3 = Color3.new(102/255, 170/255, 12/255) elseif (Humanoid.Health / Humanoid.MaxHealth) >= .4 and (Humanoid.Health / Humanoid.MaxHealth) < .6 then Color.BackgroundColor3 = Color3.new(255/255, 255/255, 0) elseif (Humanoid.Health / Humanoid.MaxHealth) >= .2 and (Humanoid.Health / Humanoid.MaxHealth) < .4 then Color.BackgroundColor3 = Color3.new(255/255, 170/255, 0) elseif (Humanoid.Health / Humanoid.MaxHealth) > 0 and (Humanoid.Health / Humanoid.MaxHealth) < .2 then Color.BackgroundColor3 = Color3.new(255/255, 0, 0) elseif (Humanoid.Health / Humanoid.MaxHealth) == 0 then Color.BackgroundColor3 = Color3.new(255/255,255/255,255/255) script.Parent.Text = ""..script.Parent.Text.."(Dead)" break end wait(.1) end
If I helped you out, make sure to hit the Accept Answer button below my character! :D
while wait() do --Add some sort of delay. If you leave it like this, the game will crash. I added a wait(). MyGuy = script.Parent.Parent.Header.Text Note = game.Workspace:FindFirstChild(MyGuy) --Since "MyGuy" is already a string, you don't need the ""..MyGuy. Health = Note.Humanoid.Health MaxHealth = Note.Humanoid.MaxHealth All = Health/MaxHealth if All >= .8 then script.Parent.BackgroundColor3 = Color3.new(0,170,0) --Color3 actually only has 2 values 1 and 0. To get 255 you need to do 170/255. elseif All >= .6 then script.Parent.BackgroundColor3 = Color3.new(102/255, 170/255, 12/255) elseif All >= .4 then script.Parent.BackgroundColor3 = Color3.new(1, 1, 0) elseif All >= .2 then script.Parent.BackgroundColor3 = Color3.new(1, 170/255, 0) elseif All > 0 then script.Parent.BackgroundColor3 = Color3.new(1, 0, 0) elseif All == 0 then script.Parent.BackgroundColor3 = Color3.new(1,1,1) script.Parent.Text = script.Parent.Text.."(Dead)" end end