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:
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:
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:
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.