#lowkey need help with hunger system
1 messages · Page 1 of 1 (latest)
at first i tried to make it so that if isRunning = true the drain rate would be twice as fast, is it conflicting with with the sprint and stamina handler?
heres the values script that creates the values when a players join
lowkey need help with hunger system
In ServerScriptService, create a Script called PlayerStats:
lua
Copy
Edit
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Value = 0
coins.Parent = leaderstats
-- Hunger Stat
local hunger = Instance.new("IntValue")
hunger.Name = "Hunger"
hunger.Value = 100 -- Full hunger
hunger.Parent = leaderstats
end)
Still in ServerScriptService, make another Script called HungerSystem:
lua
Copy
Edit
local players = game:GetService("Players")
while true do
wait(10) -- every 10 seconds
for _, player in ipairs(players:GetPlayers()) do
local stats = player:FindFirstChild("leaderstats")
if stats then
local hunger = stats:FindFirstChild("Hunger")
if hunger then
hunger.Value = hunger.Value - 1 -- lose 1 hunger
-- If Hunger <= 0, damage player
if hunger.Value <= 0 then
hunger.Value = 0 -- don't go negative
if player.Character and player.Character:FindFirstChild("Humanoid") then
player.Character.Humanoid:TakeDamage(5) -- 5 damage every 10s
end
end
end
end
end
end
- Make a Hunger GUI
In StarterGui, create a new ScreenGui called HungerGui, and inside it add a TextLabel:
ScreenGui (HungerGui)
TextLabel (HungerText)
Settings for the TextLabel:
Text: Hunger: 100
Position: {0, 20}, {0, 80}
Size: {0, 200}, {0, 50}
BackgroundTransparency: 1
TextScaled: true
(You can customize the font/color if you want!)
Inside the HungerText, add a LocalScript:
lua
Copy
Edit
local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")
local hunger = leaderstats:WaitForChild("Hunger")
local textLabel = script.Parent
hunger.Changed:Connect(function()
textLabel.Text = "Hunger: " .. hunger.Value
end)
textLabel.Text = "Hunger: " .. hunger.Value
this doesnt really solve my issue
whats ur issue
im just trying to make it so that while the isRunning boolean value is true (or in non coding terms, the player is running) hunger drains faster
you would know if you read it
its all functional, ive just had a hard time adding that mechanic to the hunger system
im replacing some code with module script stuff
so that all values are represented as variables from a module script
i like the part where if your hunger hits 0, you are guaranteed to die even if you eat something after hunger hits 0
😳 ... gosh
bro i gotta learn how to fix stuff like that,
maybe make it a function and do a return statement if suddenly the hunger bar increases
functions and parameters still trip me out when it comes to where i have to use them and how i even implement them
i simply made it a while statement, will that fix it (i havent added a food item yet so i cant test it out)
ye you could just fix it but adding another condition to the repeat until loop
repeat
---blah blah blah
until hunger > 0 or humanoid.Health == 0
ye no need for extra stuff
also
if your hunger ever runs out
your draining system will stop working
the player will need to be reset
no need really you can just do
while task.wait(40) do
if hunger > 0 then hunger -= 1 end
end
as for how you would go about the health draining
first of all your not using your running value properly, your just setting it to a bool true
what you should be doing is
local isRunning = RunningValue
--Then update it like this
isRunning.Value = true
isRunning.Value = false
back to the actually doing the draining while running, you can just check in the main drain script if the player is running, and continue if so
and in your running script you could do a function
which would decrease the hunger of the player every like 10 seconds instead or something
repeat
hunger -= 10
until task.wait(10) and isRunning
Overall, you do not need to spam function in your code unless your gonna use that code more than one time throughout your script, or your gonna use module's to organize, so basically instead of having a 1000 line script you can just divide its components into functions in modules
@visual rapids anything else you need mate?
ill notify you
** You are now Level 6! **
sounds good, good luck
and what I mean by continue here is this
while true do
if something then continue end
--Continuing will skip the iteration of the while loop
end
ive got a question/questions, where do i place any of this code?. i'd feel bad if you had to become hand holdy for this
im gonna code a simple food item so i can experiment better
this is just changing the variable
this is your main hunger
And here well, I'd also like to say that you need to put it in a coroutine or wrap it in a task.spawn
because as you may already know, while loops stop the code after them till, a condition is met
so what task.spawn does and this is a bit of a simplification is that it allows the while loop to run, while letting the other code work as well
god i suck at coding i cant do this for shit

i made a simple apple that works all but updating the hunger value to go up by 5
whatever the problem with the isrunning variable is probably the same thing going on with this..
and i still have no fucking clue what the problem is
Cuz yo scripts are AI generated
No not really
I’ve been looking at a lot of stuff online
Albeit my friend did help with the code of the stamina
Your friend ?
Where did you find the code ?
Nah it aint important
But why should we fix a code that is not entirely yours ?
why did you even text here
if you ain't helping
then leave
To ask a question
I don’t want to waste my time helping if the code is not from him
When you help your goal should not necessarily making the OP’s script work but making them actually learn
then did you even chat here in the first place 🤦♂️
Because I might have the intention to help.
Bro your contradicting yourself, you say you have no intention of helping because the code is not written by him, and then you say you might have the intention to help, like tf are you on about
I said I’d not help if the code was not written by him
I just want to know if it’s his code
And how is that any of your bussiness, and in any case, since you want him to learn, why don't you explain the code to him?
Everybody uses other people's code all the time
I use it now and then, you probably use it now and then, the hundred's of games which depend on Profile store for their datastores
I’ll answer to that because it’s pretty interesting
I never use pieces of codes that are not mine
Not even profilestore, fastcast, etc.
I make my systems myself
From scratch
Never asked
and your going of the topic
You not using other people's script doesn't make you the best at scripting, some people just like to save some times and be more effecient
If in your case you write your scripts from scratch then good for you, but just know that doesn't make you any better than me or any other person really, especially if your systems are shit
and not reusable
In CharacterAdded
You have a parameter
And it’s the Character
So you can do
Player.CharacterAdded:Connect(function(Character)
print(Character.Name)
end)
Hm
I have been coding for barely under a month so yeah
I’m glad I can make a stamina system and generally I’ve been soaking up as much coding information as I can
Like a sponge