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

Changing the Color of Gui wont work why??

Asked by 5 years ago
player = game.Players.LocalPlayer
char = player.Character or player.CharacterAdded:Wait()
Human = char:WaitForChild('Humanoid')
healthframe = script.Parent
Imagelabel = script.Parent.Parent.Parent.ImageLabel

Human.HealthChanged:Connect(function(Hp)
    healthframe.Size = UDim2.new(Hp/Human.MaxHealth,0,0,0)

end)

if Human.Health < 70 then 
    Imagelabel = script.Parent.Parent.Parent.ImageLabel
    Imagelabel.ImageColor3 = Color3.new(111,111,111)

end

2 answers

Log in to vote
1
Answered by
ABK2017 406 Moderation Voter
5 years ago

Color3.new is followed by 0-1,0-1,0-1. You are probably looking for Color3.fromRGB which is followed by 0-255,0-255,0-255.

if Human.Health < 70 then 
    Imagelabel = script.Parent.Parent.Parent.ImageLabel
    Imagelabel.ImageColor3 = Color3.fromRGB(111,111,111)

end
Ad
Log in to vote
1
Answered by
ee0w 458 Moderation Voter
5 years ago

This is where an if else statement comes in handy.


Right now, if your health goes below 70, it will stay that colour forever, since you haven't set it back. You can easily set it back by using an if else statement:

if x == 5 then
    print("x is equal to five!")
else
    print("x is not equal to five")
end

I've also notice you used Color3.new. It only takes values ranging from 0 to 1. Consider using Color3.fromRGB or dividing by 255.

Plugging this into your code, you get something like this:

local player = game.Players.LocalPlayer
local char = player.Character or local player.CharacterAdded:Wait()
local Human = char:WaitForChild('Humanoid')
local healthframe = script.Parent
local Imagelabel = script.Parent.Parent.Parent.ImageLabel
local defaultColor = Imagelabel.ImageColor3


Human.HealthChanged:Connect(function(Hp)
    healthframe.Size = UDim2.new(Hp/Human.MaxHealth,0,0,0)
end)

if Human.Health < 70 then
    Imagelabel.ImageColor3 = Color3.fromRGB(111,111,111)
else
    Imagelabel.ImageColor3 = defaultColor
end

Hope I helped!

Answer this question