I’m trying to make my hunger system decrease faster whenever the player starts sprinting. To do this, I added a BoolValue called IsRunning to the player’s LocalPlayer. The sprinting script enables it. I wrote some basic code in the hunger script that detects if the player’s IsRunning value is true. If it is, then it will change the decrease time value that’s inside of the hunger script to something lower, so the hunger decreases faster. The problem is that it doesn’t seem to work. Help?
Edit: whoops wrong code, fixed now
Code:
local HG = script.Parent
local Text = HG:WaitForChild("Calories"):WaitForChild("Text")
local Plr = game.Players.LocalPlayer
repeat wait() until Plr.Character
local Hum = Plr.Character:WaitForChild("Humanoid")
local MaxHunger = 18.1
local DecreaseRate = script.DecreaseValue -- the lower the number, the faster it decreases. the higher the number, the longer it will take before decreasing
local HungerValue
local RunDecrease = Plr:FindFirstChild("IsRunning")
if Plr:FindFirstChild("HungerVal") then
HungerValue = Plr.HungerVal
HungerValue.Value = MaxHunger
else
Instance.new("IntValue", Plr).Name = "HungerVal"
Plr.HungerVal.Value = MaxHunger
HungerValue = Plr.HungerVal
end
HungerValue.Changed:connect(function()
Text:TweenSize(UDim2.new(HungerValue.Value/MaxHunger,0, 0.55,0), "Out", "Linear", .5, true)
end)
while wait(DecreaseRate) do
if HungerValue.Value - 1 >= 0 then
HungerValue.Value = HungerValue.Value - 1
end
if RunDecrease.Value == true then
script.DecreaseValue.Value = 1 -- decreases the time it takes for hunger/thirst to decrease.
end
if HungerValue.Value == 0 then
repeat wait(1)
Hum.Health = Hum.Health - 2
until HungerValue.Value > 0 or Hum.Health <= 0
end
end
is this what you want to fix? or i am misunderstanding something here
Edit: Incase the second video is expected behavior here is the edited code
local HG = script.Parent
local Text = HG:WaitForChild("Calories"):WaitForChild("Text")
local Plr = game.Players.LocalPlayer
repeat wait() until Plr.Character
local Hum = Plr.Character:WaitForChild("Humanoid")
local MaxHunger = 18.1
local DecreaseRate = script.DecreaseValue -- the lower the number, the faster it decreases. the higher the number, the longer it will take before decreasing
local HungerValue
local RunDecrease = Plr:FindFirstChild("IsRunning")
--[[if not RunDecrease then --Create IsRunning Boolean if it not exist
Instance.new("BoolValue", Plr).Name = "IsRunning"
Plr.IsRunning.Value = true
RunDecrease = Plr.IsRunning
end]]
if Plr:FindFirstChild("HungerVal") then
HungerValue = Plr.HungerVal
HungerValue.Value = MaxHunger
else
Instance.new("IntValue", Plr).Name = "HungerVal"
Plr.HungerVal.Value = MaxHunger
HungerValue = Plr.HungerVal
end
HungerValue.Changed:connect(function()
HG:TweenSize(UDim2.new(HungerValue.Value/MaxHunger,0, 0.55,0), "Out", "Linear", .5, true)
end)
while wait(script.DecreaseValue.Value) do
if HungerValue.Value - 1 >= 0 then
HungerValue.Value = HungerValue.Value - 1
end
if RunDecrease.Value == true then
script.DecreaseValue.Value = 1 -- decreases the time it takes for hunger/thirst to decrease.
end
if HungerValue.Value == 0 then
repeat wait(1)
Hum.Health = Hum.Health - 2
until HungerValue.Value > 0 or Hum.Health <= 0
end
end