0
votes

So for starters I have

playerStats (module script) game.StarterPlayer.StarterCharacterScripts

local module = {}

module.hunger = 0
module.thirst = 100

return module

In order for you to not start losing health hunger OR thirst has to be above 0. Right now I have it at zero so I can test the food item that I made that is suppose to add 20 hunger. That does work and stops health loss, BUT it does not update the foodBar UI. (I don't have anything for thirst yet).

The script I have for updating screen and the food item is:

food item (server-side) workspace.Food.ClickDetector

local food = script.Parent.Parent
local PlayerStats = require(game.StarterPlayer.StarterCharacterScripts.playerStats)
local gain = 2
local playerFunctions = require(game.StarterPlayer.StarterCharacterScripts.playerFunctions)

script.Parent.MouseClick:Connect(function() 
    food:remove()
    PlayerStats.hunger+=gain
    print("Food on click : " .. PlayerStats.hunger)
    playerFunctions.updateFood()
end)

update foodBar (module script) game.StarterPlayer.StarterCharacterScripts

local module = {}

module.updateFood = function() 
    local gui = game.StarterGui.ScreenGui
    local PlayerStats = require(game.StarterPlayer.StarterCharacterScripts.playerStats)
    gui.foodBar.Text = "Hunger: " .. PlayerStats.hunger
end

return module

I don't think its the function to update the foodbar because it works at the start in my other script used to check the player stats

local data = require(game.StarterPlayer.StarterCharacterScripts.playerStats)
local player = script.Parent
local humanoid = player:WaitForChild("Humanoid");
local playerFunctions = require(script.Parent.playerFunctions)

task.spawn(function()
    while data.hunger <= 0 or data.thirst <= 0 do
        wait(0.1)
        humanoid.Health-=1
    end
end)

humanoid.Died:Connect(function()
    data.hunger = 100
    data.thirst = 100
end)

while true do
    wait(0.1)
    playerFunctions.updateFood()
end

Yes, I know I put a lot of code here, but I am very new to Lua and have no idea of what this could possibly be so I wanted to make sure you could see everything just in case its something unexpecting.

1

1 Answers

0
votes

The reason why the hunger bar does not update is because you're updating StarterGui (the instance that gives out the Gui) instead of PlayerGui (PlayerGui is inside the Player's instance)
Also, don't update the playerStats module inside StarterPlayer, it will give those stats to all players. Instead, update the module inside of PlayerScripts (which is also located inside of the Player's instance) and require that module instead.