#lowkey need help with hunger system

1 messages · Page 1 of 1 (latest)

visual rapids
#

stamina and hunger are functional and implemented, but my problem is I'm trying to make it so that hunger drains faster when you're running but that hasnt been working out, do i need to add a new value or is something wrong?

#

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

mellow sparrow
#

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

#
  1. 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

visual rapids
#

this doesnt really solve my issue

mellow sparrow
#

whats ur issue

visual rapids
#

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

visual rapids
#

im replacing some code with module script stuff

#

so that all values are represented as variables from a module script

little warren
visual rapids
#

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

visual rapids
short orchid
#
repeat
---blah blah blah
until hunger > 0 or humanoid.Health == 0
visual rapids
#

how so?

#

oh

short orchid
#

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

visual rapids
#

okay thanks

#

ill try to figure it out with functions?

short orchid
#

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?

visual rapids
#

ill notify youupvote

signal stratusBOT
#

studio** You are now Level 6! **studio

short orchid
#

sounds good, good luck

short orchid
visual rapids
#

im gonna code a simple food item so i can experiment better

short orchid
short orchid
#

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

visual rapids
#

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

midnight glade
visual rapids
#

No not really

#

I’ve been looking at a lot of stuff online

#

Albeit my friend did help with the code of the stamina

midnight glade
#

Your friend ?

#

Where did you find the code ?

#

Nah it aint important

#

But why should we fix a code that is not entirely yours ?

short orchid
#

if you ain't helping

#

then leave

midnight glade
#

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

short orchid
midnight glade
short orchid
midnight glade
#

I just want to know if it’s his code

short orchid
#

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

midnight glade
#

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

short orchid
#

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

midnight glade
#

@visual rapids

#

Do you know what is a parameter ?

visual rapids
#

Yeah

#

I haven’t used them super often

midnight glade
#

In CharacterAdded

#

You have a parameter

#

And it’s the Character

#

So you can do

Player.CharacterAdded:Connect(function(Character)
print(Character.Name)
end)
visual rapids
#

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

midnight glade
#

Where is the HungerDrainHandler script located ?

#

Inside of StarterPlayerScripts or the StarterCharacterScripts

visual rapids
#

Starter player

#

Scripts