#Change Food and Water Values Every Minute

9 messages · Page 1 of 1 (latest)

random halo
#

I made a GUI that display's a current player's health, food, and thirst level. I have the values stored in the player's StarterGUI. Every minute I want to subtract the Food and Water values by 3, which will automatically update the UI.

lost plank
#
local food = script.Parent.Parent.food.Value
while true do
    script.Parent.foodnumber.Text = food
    food = food - 3 
    task.wait(60)
end
#

you really don't need a number value to do this, you can just replace the food variable there with a normal number

random halo
#

I don't need the script.Parent.foodnumber.Text = food right?

#
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local Healthbar = script.Parent.healthbar
local Foodbar = script.Parent.foodbar
local Waterbar = script.Parent.waterbar

local function UpdateHealthbar()
    local health = math.clamp(Humanoid.Health / Humanoid.MaxHealth, 0, 1)
    Healthbar.Size = UDim2.fromScale(health, 0.30)
    script.Parent.healthnumber.Text = Humanoid.Health
end

local function UpdateFoodbar()
    local food = math.clamp(script.Parent.Parent.food.Value / 100, 0, 1)
    Foodbar.Size = UDim2.fromScale(food, 0.30)
    script.Parent.foodnumber.Text = script.Parent.Parent.food.Value
end

local function UpdateWaterbar()
    local water = math.clamp(script.Parent.Parent.water.Value / 100, 0, 1)
    Waterbar.Size = UDim2.fromScale(water, 0.30)
    script.Parent.waternumber.Text = script.Parent.Parent.water.Value
end


UpdateHealthbar()
UpdateFoodbar()
UpdateWaterbar()

Humanoid:GetPropertyChangedSignal("Health"):Connect(UpdateHealthbar)
Humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(UpdateHealthbar)


local diffValues = script.Parent.Parent

diffValues.water:GetPropertyChangedSignal("Value"):Connect(UpdateWaterbar)
diffValues.food:GetPropertyChangedSignal("Value"):Connect(UpdateFoodbar)
#

I already automatically update it, so I shouldn't need that right?

#

@lost plank Hey, for some reason I can't edit Humanoid.Health even though I changed it above?? Here's my code I'm trying

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local Healthbar = script.Parent.healthbar
local Foodbar = script.Parent.foodbar
local Waterbar = script.Parent.waterbar

local food = script.Parent.Parent.food
local water = script.Parent.Parent.water

-- Functions --
local function UpdateHealthbar()
    local health = math.clamp(Humanoid.Health / Humanoid.MaxHealth, 0, 1)
    Healthbar.Size = UDim2.fromScale(health, 0.30)
    script.Parent.healthnumber.Text = Humanoid.Health
end

local function UpdateFoodbar()
    local food = math.clamp(script.Parent.Parent.food.Value / 100, 0, 1)
    Foodbar.Size = UDim2.fromScale(food, 0.30)
    script.Parent.foodnumber.Text = script.Parent.Parent.food.Value
end

local function UpdateWaterbar()
    local water = math.clamp(script.Parent.Parent.water.Value / 100, 0, 1)
    Waterbar.Size = UDim2.fromScale(water, 0.30)
    script.Parent.waternumber.Text = script.Parent.Parent.water.Value
end
-- End --

-- Updates Bars --
UpdateHealthbar()
UpdateFoodbar()
UpdateWaterbar()
-- End --

-- Detects if Values Update --
Humanoid:GetPropertyChangedSignal("Health"):Connect(UpdateHealthbar)
Humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(UpdateHealthbar)
water:GetPropertyChangedSignal("Value"):Connect(UpdateWaterbar)
food:GetPropertyChangedSignal("Value"):Connect(UpdateFoodbar)
-- End --

-- Subtracts Food and Water --
while true do
    if food.Value == 0 then
        continue
    else
        food.Value = food.Value - 1
    end
    
    if water.Value == 0 then
        continue
    else
        water.Value = water.Value - 1
    end
    
    

    --if food.Value or water.Value == 0 then
        --Humanoid.Heath = Humanoid.Health - 6
    --end
    task.wait(0.25)
end
-- End --
#

And also after testing it, it doesn't even subtract from food or water