Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

GUI connection and color issues?

Asked by 9 years ago

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 =/

0
@BSI 1/30 of a second is how fast animations and movies roll at. If you take an animation and but wait(0.1) at it then It would be way too slow. GIFs move at around 1/30 of a second. EzraNehemiah_TF2 3552 — 9y
0
oh, good to know, thanks BSIncorporated 640 — 9y

2 answers

Log in to vote
2
Answered by
Discern 1007 Moderation Voter
9 years ago

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

1
Your script is correct but It could be WAY shorter like my script. You see, to add a wait into a loop like while true do, you could do instead: while wait(.1) do. Also wait() is faster than wait(.1). 255/255 is 1 so you could do Color3.new(1,1,1) for white. in line 14 delete the "".. before script.Parent.Text. Also delete ""..MyGuy with MyGuy. Since it is already a string. Just a tip to make your EzraNehemiah_TF2 3552 — 9y
1
I don't like to use wait() because I tend to not think it's fast enough, but it's probably just me, lol. As for the other things, I agree that they could definitely make the script shorter. Discern 1007 — 9y
1
script better. BTW you're almost a scholar. I'll be here to celebrate with you. PS Isn't wait() just as fast as RunService.HeartBeat? And also wait() is much faster than wait(.1) because wait() is practically wait(0.029999999999999) or 1/30 of a second. EzraNehemiah_TF2 3552 — 9y
2
Fantastic, thanks! Discern 1007 — 9y
View all comments (20 more)
2
And I suppose it is, but it's just an idiosyncrasy of mine to put wait(.1) instead of wait(). Lol. Discern 1007 — 9y
1
One person tried to troll me buy spamming the Thumbs down button on one of my answers and It spammed my notification with junk. EzraNehemiah_TF2 3552 — 9y
1
That's absolute trash Discern 1007 — 9y
1
We should probably have a conversation through Private Message. EzraNehemiah_TF2 3552 — 9y
0
uhh, idk why, but it still wont work...the gui and guy start out in the player's backpack and then get placed with a tool... BSIncorporated 640 — 9y
1
@BSIncorporated Hopefully I fixed it, I didn't catch an error earlier. Discern 1007 — 9y
1
@Discern BTW, you forgot to remove the "".. at line 12. EzraNehemiah_TF2 3552 — 9y
0
ok, one more error, just change the "All" after the "ands" to match, it says "All doesnt exist" BSIncorporated 640 — 9y
1
@BSIncorporated Fixed it. Please reply if I missed anything else! Discern 1007 — 9y
0
Something is still wrong, maybe the connection to the Human in workspace? Idk BSIncorporated 640 — 9y
1
Fixed it (hopefully!!!!!!!!!!!!!!!!!!!!!) Discern 1007 — 9y
0
Im sorry but it still just stays as the original color, maybe Ill try showing a value instead of a color (if we cant figure this out) BSIncorporated 640 — 9y
0
the problem is it cant find "MyGuy" once it's put into workspace... BSIncorporated 640 — 9y
1
Is this script inside a LocalScript or a regular Script? Discern 1007 — 9y
1
Fixed it once and for all. Make sure that the script is inside of a Script, NOT a LocalScript. Discern 1007 — 9y
0
WOOOOOOOOOOO IT WORKED! LOL! I disabled the script, then made it activate when the object is placed :D everything is working now. Thanks so much :D BSIncorporated 640 — 9y
0
I have a question pertaining to the beginning of this comment string: Why do people care to make wait shorter than 1/30 of a second, what difference will it ever make? BSIncorporated 640 — 9y
0
Discern, the funny thing is that I still got more Rep from my answer. Probably because It was working... EzraNehemiah_TF2 3552 — 9y
0
@LordDragonZord NOW it works because you edited all the "ifs". You basically copied what I did in my script with the ifs. Discern 1007 — 9y
0
...you guys were both very helpful, please don't argue about it. BSIncorporated 640 — 9y
Ad
Log in to vote
2
Answered by 9 years ago
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

Answer this question