Hello everyone!! I’m trying to make a hunger system where if your hunger goes to 0 then your health starts decreasing until you eventually die.
The problem is, after the players hunger goes to 0 and they die, the next time they respawn, their health doesn’t start decreasing after their hunger goes to 0.
Here is a video of what I mean:
robloxapp-20220314-1822544.wmv (4.9 MB)
I’ve tried printing the humanoid/health etc. and everything printed perfectly. I’m clueless as of why it doesn’t work.
Here is the script, it’s a little long, but I think the only part that matters is after line 128 where I commented “IT DOESN’T WORK HERE”.
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")
local Remotes = ReplicatedStorage:WaitForChild("Events")
local MainEvent = Remotes:WaitForChild("Eat")
-- HUNGER
local MAX_HUNGER = 100
local STARTING_VALUE = MAX_HUNGER
local DECREASE_AMOUNT = 20
local HUNGRY_AMOUNT = 50
local MIN_HUNGER = DECREASE_AMOUNT
local DECREASE_TIME = 2
local START_DECREASE_TIME = DECREASE_TIME * 2
-- SATURATION
local SATURATION_STARTING_VALUE = 0
local SATURATION_DECREASE_AMOUNT = 1
local SATURATION_DECREASE_TIME = DECREASE_TIME / 4
local MAX_SATURATION = 100
local MIN_SATURATION = SATURATION_DECREASE_AMOUNT
-- STARVE
local STARVE_INTERVAL = 0.5
local STARVE_DAMAGE = 10
-- EFFECTS
local DEFAULT_SPEED = 16
local SOUR_SPEED = 20
local SOUR_INTERVAL = 5
local EAT_INTERVAL = 3
local DEBOUNCE = false
--
local Foods = {
["Carrot"] = {["Sour"] = false, ["HungerIncrement"] = 2, ["Saturate"] = 20},
["Apple"] = {["Sour"] = true, ["HungerIncrement"] = 3, ["Saturate"] = 35},
["Lemon"] = {["Sour"] = true, ["HungerIncrement"] = 1, ["Saturate"] = -10},
["Orange"] = {["Sour"] = true, ["HungerIncrement"] = 3}, ["Saturate"] = 30,
}
local Drinks = {
["Soda"] = {["Sweet"] = true, ["SpeedIncrement"] = 5},
["Lemonade"] = {["Sweet"] = false, ["SpeedIncrement"] = 3},
["Orange Juice"] = {["Sweet"] = true, ["SpeedIncrement"] = 4},
}
--
local Full = false
local Hungry = false
local Starving = false
local Saturated = false
local State = nil
function RBX_ASSET_ID(number)
if number then
number = tostring(number)
return "rbxassetid://".. number
end
return false
end
function new(Player)
-- Check if there is a player data folder, if not, create one
local PlayerData = nil
if not Player:FindFirstChild("playerData") then
PlayerData = Instance.new("Folder")
PlayerData.Name = "playerData"
PlayerData.Parent = Player
else PlayerData = Player:WaitForChild("playerData") end
-- Create the hunger value
local Hunger = Instance.new("IntValue")
Hunger.Name = "Hunger"
Hunger.Value = STARTING_VALUE
Hunger.Parent = PlayerData
-- Create the saturation value
local Saturation = Instance.new("IntValue")
Saturation.Name = "Saturation"
Saturation.Value = SATURATION_STARTING_VALUE
Saturation.Parent = PlayerData
-- Set attributes so variables can be accessed by other scripts
local function setAttributes(HUMANOID)
if HUMANOID then
HUMANOID:SetAttribute("MaxHunger", MAX_HUNGER)
HUMANOID:SetAttribute("MinHunger", MIN_HUNGER)
end
end
local Character = Player.Character
if not Character or not Character.Parent then
Character = Player.CharacterAdded:Wait()
end
local Humanoid = Character:WaitForChild("Humanoid")
setAttributes(Humanoid)
--
task.wait(START_DECREASE_TIME) -- Wait the start decrease time amount before the players health starts decreasing
while task.wait() do
-- HUNGER (Uses two if statements to check if saturated to make the code look less messy)
if not Saturated then
if Hunger.Value >= MIN_HUNGER then
-- DECREASE
task.wait(DECREASE_TIME)
Full = false
Hunger.Value -= DECREASE_AMOUNT
if Hunger.Value <= HUNGRY_AMOUNT then
Hungry = true
end
else
-- IT DOESN'T WORK HERE
-- STARVING
task.wait(STARVE_INTERVAL)
Starving = true
Hunger.Value = 0 -- Makes sure that the hunger cannot go less than 0
if Humanoid then
Humanoid:TakeDamage(STARVE_DAMAGE)
end
end
end
-- SATURATION
if Saturated then
if Saturation.Value >= MIN_SATURATION then
task.wait(SATURATION_DECREASE_TIME)
Saturation.Value -= SATURATION_DECREASE_AMOUNT or 2
else Saturation.Value = 0 Saturated = false end
if Saturation.Value - SATURATION_DECREASE_AMOUNT == MIN_SATURATION then
task.wait(DECREASE_TIME)
Saturation.Value = 0
end
end
Humanoid.Died:Connect(function()
Saturation.Value = MAX_SATURATION
Hunger.Value = MAX_HUNGER
end)
end
end
Players.PlayerAdded:Connect(new)
function Activated(Player, Name, FoodInstance)
-- Define the players character
local Character = Player.Character
if not Character or not Character.Parent then
Character = Player.CharacterAdded:Wait()
end
local Humanoid = Character:WaitForChild("Humanoid")
-- Food
if Foods[Name.Value] then
local Food = Foods[Name.Value]
local PlayerData = Player:FindFirstChild("playerData")
local Hunger = PlayerData:FindFirstChild("Hunger")
local Saturation = PlayerData:FindFirstChild("Saturation")
if PlayerData and Hunger and not DEBOUNCE then
DEBOUNCE = true
-- Eating animation
if FoodInstance then
local Animator = Humanoid:WaitForChild("Animator")
local EatAnimation = Instance.new("Animation")
EatAnimation.AnimationId = RBX_ASSET_ID(9039489111)
local EatAnimationTrack = Animator:LoadAnimation(EatAnimation)
EatAnimationTrack:Play()
Debris:AddItem(FoodInstance, 3)
task.wait(EAT_INTERVAL) DEBOUNCE = false
end
-- Increase hunger
if not (Hunger.Value + Food["HungerIncrement"] >= MAX_HUNGER) then
Hunger.Value += Food["HungerIncrement"]
else Hunger.Value = MAX_HUNGER Full = true end
-- Increase saturation
if Food["Saturate"] then
Saturated = true
if not (Saturation.Value + Food["Saturate"] >= MAX_SATURATION) then
Saturation.Value += Food["Saturate"]
else Saturation.Value = MAX_SATURATION end
end
-- Check if the food is sour
if Food["Sour"] then
if Humanoid.WalkSpeed == SOUR_SPEED then repeat task.wait() until Humanoid.WalkSpeed ~= SOUR_SPEED end
Humanoid.WalkSpeed = SOUR_SPEED
task.wait(SOUR_INTERVAL)
Humanoid.WalkSpeed = DEFAULT_SPEED
end
end
end
-- Drink
if Drinks[Name.Value] then
local Drink = Drinks[Name.Value]
end
end
MainEvent.OnServerEvent:Connect(Activated)
Any help is appreciated, thank you so much!! If you want any sort of explanation of the code or more information, please ask me.