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

How would I make so the server constantly checks if my health is low?

Asked by
Hydrogyn 155
4 years ago
Edited 4 years ago

hi,

how would I make it so the server always checks whether the current_health value is below a certain percentage? Whenever I regenerate my health, the health bar does not update therefore it's still yellow instead of green! Plus sometimes <= or >= don't work.

local health = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
local bar = script.Parent.Parent
local current_health = health.Health / 100
local runService = game:GetService("RunService")

health.HealthChanged:connect(function()
runService.Stepped:connect(function()
bar.Size = UDim2.new((health.Health / 100) * 1,0,1,0)   

 if current_health >=.51 then
    --green
    bar.BackgroundColor3 = Color3.fromRGB(79, 222, 1)
    bar.BorderColor3 = Color3.fromRGB(61, 172, 2)
    print'green'
 elseif current_health <=.5 then
    -- yellow
    bar.BackgroundColor3 = Color3.fromRGB(222, 171, 42)
    bar.BorderColor3 = Color3.fromRGB(173, 133, 32)
    print'yellow'
 elseif current_health <=.3 then
    --red
    bar.BackgroundColor3 = Color3.fromRGB(222, 75, 55)
    bar.BorderColor3 = Color3.fromRGB(166, 55, 41)
    print'red'
--  

end
end)
end)



thanks, hydrogyn

0
is there a reason you divide the current_health by 100? MageMasterHD 261 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago
local health = game.Players.LocalPlayer.Character:WaitForChild("Humanoid")
local bar = script.Parent.Parent
local runService = game:GetService("RunService")

health.HealthChanged:connect(function(new_health)

    local current_health = new_health / 100
    bar.Size = UDim2.new((new_health / 100) * 1,0,1,0)   

     if current_health >=.51 then
        --green
        bar.BackgroundColor3 = Color3.fromRGB(79, 222, 1)
        bar.BorderColor3 = Color3.fromRGB(61, 172, 2)
        print('green')
     elseif current_health <=.5 and current_health >=.3 then
        -- yellow
        bar.BackgroundColor3 = Color3.fromRGB(222, 171, 42)
        bar.BorderColor3 = Color3.fromRGB(173, 133, 32)
        print('yellow')
     elseif current_health <=.3 then
        --red
        bar.BackgroundColor3 = Color3.fromRGB(222, 75, 55)
        bar.BorderColor3 = Color3.fromRGB(166, 55, 41)
        print('red'9)
    end

end)
0
thanks man! :) Hydrogyn 155 — 4y
Ad
Log in to vote
0
Answered by
Geobloxia 251 Moderation Voter
4 years ago

Do a while true do loop with a wait(0.1) inside the script

Answer this question