How to create a RPG Hunger System

Introduction

Are you looking to create an RPG style game? Are you creating some sort of survival game? This is the tutorial for you! Today, I will be teaching you how to create a hunger system. The functions of this system will include:

  • Eating food to fill your hunger bar
  • Losing health when your hunger bar is at zero.

Getting Started

First, let’s map out the components of this system. First, create a tool, the name does not matter, but I will call it “Food.” Create a new part, and place it inside of the tool, and name it “Handle.” This will allow the player to hold the tool in their hand. Inside of the tool, insert a local script and a module script, these will be used to fire events to the server that will allow for the overall function of this system. Your tool should now look like this:

Capture
If you want to make a player spawn with the food, you can put it into StarterPack, otherwise you can place it into Lighting.

In order for the client to communicate to the server, an event needs to be fired. In order to fire an event, you will need a RemoteEvent. Right click on ReplicatedStorage, and insert a RemoteEvent. This RemoteEvent enables a local script, which is client sided, to communicate to a script, which is server sided. Many times, you will have many RemoteEvents being used in a game. This is where folders come in handy. Insert a folder into ReplicatedStorage, and insert your RemoteEvent into the folder. You can name the folder and the event whatever you would like. Your ReplicatedStorage should look as such:
125125215215125
Now that we have our RemoteEvent set up, we need a script for the local script to communicate with. Inside of ServerScriptService, insert a script. You can name this script whatever you want, I will name it “HungerScript”

Your explorer should now look like this, with all the inserted components:
asdwe

Creating the Eating Function

Now that we have set up the basics, we will begin scripting.
We want to make it so that, when the player eats the food, their hunger bar goes up. First, we need to make sure that when a player clicks with the tool equipped, we fire the RemoteEvent. To do this, open the LocalScript in your tool. We need to reference the ModuleScript first, which will be what fires this request to the server when a player clicks.

local module = require(script.Parent:WaitForChild("ModuleScript"))
local plr = game.Players.LocalPlayer

script.Parent.Activated:Connect(function()
	module.EatFood()
end)

When a player clicks while they have the food item equipped, a message is sent to the module telling it to fire the RemoteEvent, so it can communicate with the server. In order for the ModuleScript to fire this event, it needs to know what it is.

local module = {}
local rs = game:GetService("ReplicatedStorage")

function module.EatFood()
	rs.RemoteEvents.EatFood:FireServer()	
end

return module

Now the event will be fired whenever a player clicks with the food tool equipped. We want the event to send a message to the script, though. In order to do this, we need to used the OnServerEvent command. We also want the player’s hunger bar to go up. In order for this to happen, we need to define the value that is Hunger.

game.Players.PlayerAdded:Connect(function(plr)
	
	local playerWellness = Instance.new("Folder")
	playerWellness.Name = "Wellness"
	playerWellness.Parent = plr
	
	local hunger = Instance.new("IntValue")
	hunger.Name = "Hunger"
	hunger.Parent = playerWellness
	hunger.Value = 100
					
	local rs = game:GetService("ReplicatedStorage")
	rs.RemoteEvents.EatFood.OnServerEvent:Connect(function(plr)
		hunger.Value = hunger.Value + 20
	end)
end)

Now, when a player clicks with the food item equipped, their hunger value will increase, making them less hungry. We need to add some parameters that allows us to control how high and how low the player’s hunger can go, though. To do this, we have to make the script check if the player’s hunger value is 100 or greater. To do this, we need to create an if statement saying that, if the hunger value is 100 or greater, to set it to 100.

if hunger.Value >= 100 then
hunger.Value = 100

(If the value matches the inequality then set it back to 100.)
Make sure to edit your script so it looks like this:

game.Players.PlayerAdded:Connect(function(plr)
	
	local playerWellness = Instance.new("Folder")
	playerWellness.Name = "Wellness"
	playerWellness.Parent = plr
	
	local hunger = Instance.new("IntValue")
	hunger.Name = "Hunger"
	hunger.Parent = playerWellness
	hunger.Value = 100
			
	local rs = game:GetService("ReplicatedStorage")
	rs.RemoteEvents.EatFood.OnServerEvent:Connect(function(plr)
		hunger.Value = hunger.Value + 20
		if hunger.Value >= 100 then
			hunger.Value = 100
			end
		end)
end)

This will add a sense of control to this system, so a player’s hunger can’t dip too high or too low. Without these parameters, the system wouldn’t have a cap, allowing players to go hours without eating food. (Which you don’t want.)

Making the Player Hungry

We just finished creating the main functions of the tool, but we never surfaced on making the player hungry. We will be using a loop so that the hunger is constantly decreasing, making the food a necessity. Create a LocalScript, and place it into StarterGui. This script will cause the hunger to constantly decrease, and create a punishment if a user gets lazy, and forgets to eat some food.

local plr = game.Players.LocalPlayer
local humanoid = plr.Character:WaitForChild("Humanoid")
while true do
	if plr.Wellness.Hunger.Value == 0 then
		humanoid.Health = humanoid.Health - 1
		wait(1)
	else
		plr.Wellness.Hunger.Value = plr.Wellness.Hunger.Value - 1
		wait(1)
		end
	end

This script causes a player’s hunger value to constantly decrease until it reaches 0, which will cause the player’s health to decrease.

Conclusion

You just learned how to make a fully functional hunger system in Roblox. This is only the main structure of this system, though. You can add your own touches to it such as a hunger GUI displaying a player’s hunger levels. I hope that this tutorial helped you out, and suited your needs.

71 Likes

Very nice tutorial! I hope you will do more!

8 Likes

This is a great guide! You could pair this with a similar script for a thirst system too by editing it a little bit. Some things that you could add if you wanted to go more in depth would be to make the players vision blurry or to slow the player down when they are low on hunger or thirst. Great and useful post though! :slightly_smiling_face:

5 Likes

This is a really good tutorial! I love how it’s easy to follow and each step is explained thoroughly with enough detail that pretty much any scripter (with at least a tiny bit of Studio knowledge) could accomplish this system. :slight_smile:

3 Likes

Wouldn’t a local script cause it to only be affected on the player’s end?

3 Likes

If you want the hunger to decrease on the Server (which you should)

Hey everyone, I just thought that everyone should know that if you come across a problem where the food fills you up all the way to the top instead of adding 20 here’s why. The hunger is decreasing on the client side meaning that for the client, the hunger value will decrease but on the server side it will still look like its 100. With filtering enabled in order to combat this problem you have to make the player hungry on the server side instead. This will resolve your issue. Inside the ServerScriptServive enter this in the script.

while true do
	wait(5) -- how often you want to make the players hunger value go down
	for i, v in pairs(game.Players:GetChildren()) do
		if v.Wellness.Hunger.Value > 0 then -- this if statement is just to make sure the player doesn't go "negative" hunger
			v.Wellness.Hunger.Value = v.Wellness.Hunger.Value - 1
            -- if you want to make the value that drops random do this instead
            v.Wellness.Hunger.Value = v.Wellness.Hunger.Value - math.random(first number, second number)  -- it will choose a random number between the two
		end
	end
end

Hope this helps!

6 Likes

do you use this in any of your games?
this actualy seems like you use this in a game or something
nice asset tho!

Nice tutorial, but you should make hunger decrease on server because otherwise there is no point in remote events. Also I don’t see a reason to use module to fire the remote.

2 Likes

I’m assuming that if you have multiple food options like let’s say an apple, banana, and an orange it may be easier to use a module function to make it easier but other than making it easier with more food options I don’t see much of a reason for it as well.

3 Likes

It would still be better to have it within that script, you can just send type of food as argument and handle it in that function. But if he prefers modules for this, why would I stop him.

5 Likes