#i'm facing a big roadblock in coding

1 messages · Page 1 of 1 (latest)

river wedge
#

i'll post the code in the later messages but i've been facing a big roadblock in making my code more functional, basically i have a working sprint and stamina system, a sorta functional hunger system and i'm making an edible item to better test out the hunger system.

i've made a module script to create values in the player character and declare those values as variables
problem is, a guy like me would think this would work and make these game mechanics able to interact with each other, but that's not the case. i want to make it so that sprinting makes hunger drain twice as fast and eating the test food regens some hunger, but those haven't worked, everything else works but not those

#
-- this is the player's sprinting and stamina system, it allows the player to move around faster at the slow cost of stamina
 
    local user_input_serv = game:GetService("UserInputService")
    local run_serv = game:GetService("RunService")
    local player_serv = game:GetService("Players")
    local player = player_serv.LocalPlayer    
player.CharacterAdded:Connect(function() --yields until the character is added
    local player_char = player.Character 
    local humanoid = player_char:WaitForChild("Humanoid")
    local animator = humanoid:WaitForChild("Animator")
    --Values
    local PlayerValues = require(script.Parent.PlayerValuesModule)
    local canRegen = PlayerValues.Values.Stamina.canRegen
    local canRun = PlayerValues.Values.Stamina.canRun
    local isRunning = PlayerValues.Values.Stamina.isRunning
    local stamina = PlayerValues.Values.Stamina.stamina
    local   
    --Animation
    local runAnim = script.run:WaitForChild("RunAnim")
    local runAnimTrack = animator:LoadAnimation(runAnim)
    local jumpAnim = script.jump:WaitForChild("JumpAnim")
    local jumpAnimTrack = animator:LoadAnimation(jumpAnim)
    --Gui
    local MainGui = player.PlayerGui:WaitForChild("MainGui")
    local stamina_number = MainGui:WaitForChild("StaminaNumber")
    
    --Default Values
    stamina.Value = 100
    canRegen = true
    canRun = true
    humanoid.WalkSpeed = 13.5
    isRunning = false
    humanoid.JumpPower = 35
    
    -- Sprint input
    

#
user_input_serv.InputBegan:Connect(function(input)
        if input.KeyCode == Enum.KeyCode.LeftShift and canRun == true and humanoid.MoveDirection.Magnitude > 0 then do
                runAnimTrack:Play()
                isRunning = true
                humanoid.WalkSpeed = 24
                humanoid.JumpPower = 45
                while stamina.Value > 0 and isRunning == true do
                    stamina.Value = stamina.Value - 1
                    wait(.2)
                    if stamina.Value == 0 then do
                            humanoid.WalkSpeed = 13.5
                            humanoid.JumpPower = 35
                            isRunning = false
                            canRun = false
                            canRegen = false
                            wait(4)
                            canRun = true
                            canRegen = true
                        end                end
                end
            end
        end
    end)
    user_input_serv.InputEnded:Connect(function(input)
        if input.KeyCode == Enum.KeyCode.LeftShift and isRunning == true then do
                runAnimTrack:Stop()
                isRunning = false
                canRegen = false
                wait(0.09)
                canRegen = true
                humanoid.WalkSpeed = 13.5
                humanoid.JumpPower = 35
                while stamina.Value < 100 and isRunning == false and canRegen == true  do
                    stamina.Value = stamina.Value + 1
                    wait(0.25)
                end
            end
        end    
    end)
    
    
    
    --Updating the stamina text every frame
    run_serv.Heartbeat:Connect(function()
        stamina_number.Text = ""..tostring(stamina.Value)
    end)
    player.CharacterAdded:Connect(function(newCharacter)
        stamina.Value = 100
        canRegen = true
        canRun = true
        humanoid.WalkSpeed = 13.5
        isRunning = false
    end)
end)

#

this is SprintStaminaHandler

#
-- this is the player's core hunger system, self explanatory

    local run_serv = game:GetService("RunService")
    local player_serv = game:GetService("Players")
    local player = player_serv.LocalPlayer
player.CharacterAdded:Connect(function()
    local player_char = player.Character 
    local humanoid = player_char:WaitForChild("Humanoid")
    -- Values
    local PlayerValues = require(script.Parent.PlayerValuesModule)
    local hunger = PlayerValues.Values.Hunger.hunger
    local isRunning = PlayerValues.Values.Stamina.isRunning
    -- Gui
    local MainGui = player.PlayerGui:WaitForChild("MainGui")
    local hunger_number = MainGui:WaitForChild("HungerNumber")
    local hunger_status = MainGui:WaitForChild("HungerStatus")
    
    -- Default Values & Updating the hunger text every fram
    hunger.Value = 20
    run_serv.Heartbeat:Connect(function()
        hunger_number.Text = ""..tostring(hunger.Value)
    end)
    -- Drain System
    while hunger.Value > 0 do
        task.wait(4)
        hunger.Value = hunger.Value - 1
    end
    
    -- Damage from starving 
while hunger.Value <= 0 do
    task.wait(2)
    repeat humanoid.Health = humanoid.Health - 10 
        task.wait(3.5) until hunger.Value > 0 or humanoid.Health <= 0
    end
end)
#

this is HungerDrainHandler

#
local PlayerValues = {}

local player_char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

-- Stamina Variables
local SprintStaminaValuesFolder = Instance.new("Folder")
SprintStaminaValuesFolder.Name = "SprintStaminaValuesFolder"
SprintStaminaValuesFolder.Parent = player_char

local boolValuecanRegen = Instance.new("BoolValue")
boolValuecanRegen.Name = "canRegenStamina"
boolValuecanRegen.Parent = SprintStaminaValuesFolder
local boolValuecanRun = Instance.new("BoolValue")
boolValuecanRun.Name = "canRun"
boolValuecanRun.Parent = SprintStaminaValuesFolder
local boolValueIsRunning = Instance.new("BoolValue")
boolValueIsRunning.Name = "isRunning"
boolValueIsRunning.Parent = SprintStaminaValuesFolder
local numValueStamina = Instance.new("NumberValue")
numValueStamina.Name = "stamina"
numValueStamina.Parent = SprintStaminaValuesFolder

-- Hunger Variables
local HungerValuesFolder = Instance.new("Folder")
HungerValuesFolder.Name = "HungerValuesFolder"
HungerValuesFolder.Parent = player_char
local numValueHunger = Instance.new("NumberValue")
numValueHunger.Name = "Hunger"
numValueHunger.Parent = HungerValuesFolder


PlayerValues.Values = {
    Stamina = {
        canRegen = boolValuecanRegen,
        canRun = boolValuecanRun,
        isRunning = boolValueIsRunning, 
        stamina = numValueStamina
    },
    Hunger = {
        hunger = numValueHunger
    }
}
return PlayerValues
#

this is PlayerValuesModule

viscid jetty
#

@river wedge Nice try, cowboy, but it’s clear this one’s ridin' right over your head.

river wedge
#

i was thinking maybe i have to put some of the new value instance code into a new serverscript?

#

im a noob at coding so yeah

viscid jetty
#

Shoot yeah, that’s mighty fine!

river wedge
#

so thats what i gotta do?

#

or if its being inserted into a localplayer should it be a localscript?

viscid jetty
river wedge
#

okay

viscid jetty
# river wedge okay

Pathfinder ain’t just a scripter — he’s the blueprint, the architect, and the spark that turns code into living, breathing worlds. Every line he writes carries the weight of a thousand lessons, stitched together with the kind of mastery only the legends ever reach.

viscid jetty
twin trellis
#

@river wedge you should add a variable HungerDrainageMultiplier to the hunger module

#

Set it by default to 1

viscid jetty
viscid jetty
#

huh?

twin trellis
#

When the player starts sprinting set HungerDrainageMultiplier to 2, when they stop sprinting set it back to 1

river wedge
#

my issue for faster hunger drain is that the isrunning variable works in the sprinting script but im guessing that for some reason the hunger script isnt detecting when the isrunning boolean is true

twin trellis
#

You should try printing the IsRunning variable in thr hunger drain script

#

Let me know if it is printing the correct value we can go from there

river wedge
#

i just did "print(isrunning)" and the output says "isRunning - Client - HungerDrainHandler:13"

#

i dont think theres anything wrong with this

#

even then i think that this and the add hunger to player character after eating thing are apart of the same issue

#

is it something relating to 2 scripts using one vairable that represents a value

twin trellis
#

Print(isRunning.Value)

river wedge
#

it says false

#

i think it does know the boolvalue

#

but when i try to make it so that if its true the hunger drains twice as fast, it doesnt work

twin trellis
#

Let me see

#
-- this is the player's core hunger system, self explanatory

    local run_serv = game:GetService("RunService")
    local player_serv = game:GetService("Players")
    local player = player_serv.LocalPlayer
player.CharacterAdded:Connect(function()
    local player_char = player.Character 
    local humanoid = player_char:WaitForChild("Humanoid")
    -- Values
    local PlayerValues = require(script.Parent.PlayerValuesModule)
    local hunger = PlayerValues.Values.Hunger.hunger
    local isRunning = PlayerValues.Values.Stamina.isRunning
    -- Gui
    local MainGui = player.PlayerGui:WaitForChild("MainGui")
    local hunger_number = MainGui:WaitForChild("HungerNumber")
    local hunger_status = MainGui:WaitForChild("HungerStatus")
    
    -- Default Values & Updating the hunger text every fram
    hunger.Value = 20
    run_serv.Heartbeat:Connect(function()
        hunger_number.Text = ""..tostring(hunger.Value)
    end)
    -- Drain System
    while hunger.Value > 0 do
        task.wait(4)
        local increment = isRunning.Value and 2 or 1
        hunger.Value -= increment
    end
    
    -- Damage from starving 
while hunger.Value <= 0 do
    task.wait(2)
    repeat humanoid.Health = humanoid.Health - 10 
        task.wait(3.5) until hunger.Value > 0 or humanoid.Health <= 0
    end
end)
#

Try this?

#

Oh and

#
-- this is the player's sprinting and stamina system, it allows the player to move around faster at the slow cost of stamina
 
    local user_input_serv = game:GetService("UserInputService")
    local run_serv = game:GetService("RunService")
    local player_serv = game:GetService("Players")
    local player = player_serv.LocalPlayer    
player.CharacterAdded:Connect(function() --yields until the character is added
    local player_char = player.Character 
    local humanoid = player_char:WaitForChild("Humanoid")
    local animator = humanoid:WaitForChild("Animator")
    --Values
    local PlayerValues = require(script.Parent.PlayerValuesModule)
    local canRegen = PlayerValues.Values.Stamina.canRegen
    local canRun = PlayerValues.Values.Stamina.canRun
    local isRunning = PlayerValues.Values.Stamina.isRunning
    local stamina = PlayerValues.Values.Stamina.stamina
    local   
    --Animation
    local runAnim = script.run:WaitForChild("RunAnim")
    local runAnimTrack = animator:LoadAnimation(runAnim)
    local jumpAnim = script.jump:WaitForChild("JumpAnim")
    local jumpAnimTrack = animator:LoadAnimation(jumpAnim)
    --Gui
    local MainGui = player.PlayerGui:WaitForChild("MainGui")
    local stamina_number = MainGui:WaitForChild("StaminaNumber")
    
    --Default Values
    stamina.Value = 100
    canRegen = true
    canRun = true
    humanoid.WalkSpeed = 13.5
    isRunning.Value = false
    humanoid.JumpPower = 35
    
    -- Sprint input
    

#

You accidentally set isRunning to true/false instead of setting isRunning.Value

river wedge
#

should i do that for all of them?

twin trellis
#
user_input_serv.InputBegan:Connect(function(input)
        if input.KeyCode == Enum.KeyCode.LeftShift and canRun == true and humanoid.MoveDirection.Magnitude > 0 then do
                runAnimTrack:Play()
                isRunning = true
                humanoid.WalkSpeed = 24
                humanoid.JumpPower = 45
                while stamina.Value > 0 and isRunning.Value == true do
                    stamina.Value = stamina.Value - 1
                    wait(.2)
                    if stamina.Value == 0 then do
                            humanoid.WalkSpeed = 13.5
                            humanoid.JumpPower = 35
                            isRunning.Value = false
                            canRun = false
                            canRegen = false
                            wait(4)
                            canRun = true
                            canRegen = true
                        end                end
                end
            end
        end
    end)
    user_input_serv.InputEnded:Connect(function(input)
        if input.KeyCode == Enum.KeyCode.LeftShift and isRunning.Value == true then do
                runAnimTrack:Stop()
                isRunning.Value = false
                canRegen = false
                wait(0.09)
                canRegen = true
                humanoid.WalkSpeed = 13.5
                humanoid.JumpPower = 35
                while stamina.Value < 100 and isRunning.Value == false and canRegen == true  do
                    stamina.Value = stamina.Value + 1
                    wait(0.25)
                end
            end
        end    
    end)
    
    
    
    --Updating the stamina text every frame
twin trellis
river wedge
#

the faster stamina drain technically works but i just want the task.wait to be faster before doing a -1 increment

twin trellis
#

Oh ik what u mean let me edit rq

#
-- this is the player's core hunger system, self explanatory

    local run_serv = game:GetService("RunService")
    local player_serv = game:GetService("Players")
    local player = player_serv.LocalPlayer
player.CharacterAdded:Connect(function()
    local player_char = player.Character 
    local humanoid = player_char:WaitForChild("Humanoid")
    -- Values
    local PlayerValues = require(script.Parent.PlayerValuesModule)
    local hunger = PlayerValues.Values.Hunger.hunger
    local isRunning = PlayerValues.Values.Stamina.isRunning
    -- Gui
    local MainGui = player.PlayerGui:WaitForChild("MainGui")
    local hunger_number = MainGui:WaitForChild("HungerNumber")
    local hunger_status = MainGui:WaitForChild("HungerStatus")
    
    -- Default Values & Updating the hunger text every fram
    hunger.Value = 20
    run_serv.Heartbeat:Connect(function()
        hunger_number.Text = ""..tostring(hunger.Value)
    end)
    -- Drain System
    while hunger.Value > 0 do
        local drainMultiplier = isRunning.Value and 0.5 or 1
        task.wait(4*drainMultiplier)
       
        hunger.Value -= 1
    end
    
    -- Damage from starving 
while hunger.Value <= 0 do
    task.wait(2)
    repeat humanoid.Health = humanoid.Health - 10 
        task.wait(3.5) until hunger.Value > 0 or humanoid.Health <= 0
    end
end)
#

This will alter the task.wait to be twice as fast if running and stay the same otherwise

#

Because 1 * 4 = 4
And 0.5*4=2

#

Btw great code!!

river wedge
#

i changed the starving block of code into a function and if statement so hopefully when i figure out how to make the hunger value go up after eating a food item it'll stop the starving function

river wedge
#

but either way thanks for the help

twin trellis
#

Im fr lol

river wedge
#

it means alot

viscid jetty
#

Of course anytime!

river wedge
twin trellis
#

Of course happy to help

viscid jetty
#

No need to thank the s2.

river wedge
#

in the eating script i printed the hungers value and it says 0 even though ingame its 20

#

i think the hunger value the eating script is using might be a different value entirely for some reason

twin trellis
#

Ill look through it

#

I cant find whats causing that

#

Maybe im missing some context or something

viscid jetty
#

If you need the s2 can come back since @twin trellis can not do it.

twin trellis
river wedge
#

in the eating script i changed the hunger varirable's declaration to wait for the child of the hunger values folder (which is also a new wait for child variable in regards to the player character)

#

this makes it so the code now identifies the hunger variable as the one the player has

#
local UIS = game:WaitForChild("UserInputService")

local FoodItem = script.Parent
local player_char = game.Players.LocalPlayer.CharacterAdded:Wait()
local humanoid = player_char:WaitForChild("Humanoid")
local animator = humanoid:WaitForChild("Animator")
local eatingSound = game.SoundService.PlayerActions:WaitForChild("Eating")

local EatingAnim = script.eat:WaitForChild("EatingAnim")
local EatingAnimTrack = animator:LoadAnimation(EatingAnim)

local PlayerValuesModule = require(game.StarterPlayer.StarterPlayerScripts.Attributes.PlayerValuesModule)
local hungerfolder = player_char:WaitForChild("HungerValuesFolder")
local hunger = hungerfolder:WaitForChild("Hunger")
print(hunger.Value)

UIS.InputBegan:Connect(function(click)
    if click.UserInputType == Enum.UserInputType.MouseButton1 and FoodItem:IsDescendantOf(player_char) and hunger.Value < 20 then
        EatingAnimTrack:Play()
        eatingSound:Play()
        task.wait(1)
    script.Parent:Destroy()    
    player_char.Health = player_char.Health + 10
    hunger.Value = hunger.Value + 5
        
    end
end)
#

it sadly still doesnt work

twin trellis
#

Maybe the player values module could be placed in starterCharacterScripts

#

So that the stamina, hunger, and other values are created each time the character is added

#

Im not sure tbh if its worth the edits though if your current code is working fine actually for the most part

viscid jetty
#

S2 will come help later.

river wedge
#

the module is placed in the attributes folder of starterplayerscripts

#

ill try that out tho

twin trellis
#

And is hunger created each time the character is added?

river wedge
#

yeah i think so

chrome fossilBOT
#

studio** You are now Level 7! **studio

twin trellis
#

These are the type of thing that probably turn out to be the smallest mistakes lol

viscid jetty
#

Well, shoot... reckon that’s just the way the tumbleweed rolls, huh?

twin trellis
#

Oh wait

river wedge
#

again i had it search for the player character's child named the hunger number value

viscid jetty
#

Can't even sort out them attribute values... what a dang waste of a good life, I tell ya.

twin trellis
#

Where you are reducing the players hunger you may have accidentally added a while true do loop inside of a .Heartbeat event

#

So i think maybe its reducing the hunger too quickly?

viscid jetty
#

Shoot, can't even get a handle on this dang S0 basics... feelin' lower than a snake's belly in a wagon rut.

twin trellis
#

Oh wait its not that my bad

river wedge
#

oh okay

#

i felt like something about that wasnt right

twin trellis
#

I just realized its bcs the way discord displays the code lol

#

U did close the .Heartbeat function it was only for updating the UI

#

I think u could change it for a hunger.Changed event but thats just optimization lol

#

Maybe u could send me a ss of the code? Im like super dependent on studios lua highlighting for some reason lol

viscid jetty
#

Well, ain't that just a fine howdy-do... two hours of our lives gone like dust in the wind. Can't wait to watch you wither like a summer weed.

river wedge
#

it does say infinite yeild possible in hungervaluesfolder

#

idk if that helps

twin trellis
#

Can i see the error printout?

#

Let me get my laptop one sec

river wedge
#

im just changing a few things so that the code works for the module now being in the startercharacter scripts

twin trellis
#

oh okay sick

river wedge
#

okay so i got stamina to work but the hunger stuff isnt working now

-- this is the player's core hunger system, self explanatory

local run_serv = game:GetService("RunService")
local player_serv = game:GetService("Players")
local player = player_serv.LocalPlayer
player.CharacterAdded:Connect(function()
    local player_char = player.Character 
    local humanoid = player_char:WaitForChild("Humanoid")
    -- Values
    local PlayerValues = require(game.StarterPlayer.StarterCharacterScripts:WaitForChild("PlayerValuesModule"))
    local hungerfolder = player_char:WaitForChild("HungerValuesFolder")
    local hunger = hungerfolder:WaitForChild("Hunger")
    local isRunning = PlayerValues.Values.Stamina.isRunning
    -- Gui
    local MainGui = player.PlayerGui:WaitForChild("MainGui")
    local hunger_number = MainGui:WaitForChild("HungerNumber")
    local hunger_status = MainGui:WaitForChild("HungerStatus")

    -- Default Values & Updating the hunger text every fram
    hunger.Value = 20
    
    -- Drain System
    while hunger.Value > 0 do
        local drainMultiplier = isRunning.Value and 0.5 or 1
        task.wait(80*drainMultiplier)
        hunger.Value -= 1
    end
    
    run_serv.Heartbeat:Connect(function()
        hunger_number.Text = ""..tostring(hunger.Value)
    end)

    -- Damage from starving 
    local function starving()
        task.wait(2)
            repeat humanoid.Health = humanoid.Health - 10 
                task.wait(3.5) until hunger.Value > 0 or humanoid.Health <= 0
        if hunger.Value > 0 then
            return
        end
    end
    if hunger.Value <= 0 then
        starving()
    end
end)
#

cant wait to get good at coding so i dont have to deal with bs like this crying

twin trellis
#

Even the greatest coders have to deal with bugs, although they do get easier to solve i think

#

I think i fixed it

#

I put it into the roblox script editor to get a better look, heres what I edited:

#
-- this is the player's core hunger system, self explanatory

local run_serv = game:GetService("RunService")
local player_serv = game:GetService("Players")
local player = player_serv.LocalPlayer
player.CharacterAdded:Connect(function(player_char)
    local humanoid = player_char:WaitForChild("Humanoid")
    -- Values
    local PlayerValues = require(player_char:WaitForChild("PlayerValuesModule"))
    local hungerfolder = player_char:WaitForChild("HungerValuesFolder")
    local hunger = hungerfolder:WaitForChild("Hunger")
    local isRunning = PlayerValues.Values.Stamina.isRunning
    -- Gui
    local MainGui = player.PlayerGui:WaitForChild("MainGui")
    local hunger_number = MainGui:WaitForChild("HungerNumber")
    local hunger_status = MainGui:WaitForChild("HungerStatus")

    -- Default Values & Updating the hunger text every fram
    hunger.Value = 20

    -- Drain System
    while hunger.Value > 0 do
        local drainMultiplier = isRunning.Value and 0.5 or 1
        task.wait(80*drainMultiplier)
        hunger.Value -= 1
    end

    run_serv.Heartbeat:Connect(function()
        hunger_number.Text = ""..tostring(hunger.Value)
    end)

    -- Damage from starving 
    local function starving()
        task.wait(2)
        repeat humanoid.Health = humanoid.Health - 10 
            task.wait(3.5) until hunger.Value > 0 or humanoid.Health <= 0
        if hunger.Value > 0 then
            return
        end
    end
    if hunger.Value <= 0 then
        starving()
    end
end)
#

i changed the character to derive from the CharacterAdded event

#

and the PlayerValues to

player_char:WaitForChild("PlayerValuesModule") 

instead of

StarterCharacterScripts:WaitForChild("PlayerValuesModule")