Creating a hunger bar throws an error

Background context, I have always hated GUI’s, The one thing on roblox I get stuck on. I recently decided to make a hunger bar using two seperate scripts (both local scripts):

  1. Depletion
    In StarterGUI
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character
local Humanoid = Player.Character:WaitForChild("Humanoid")
while true do
	wait()
	script.Parent.Size = UDim2.new(0, script.Parent.Size.X.Offset -1, 0, 13)
	if script.Parent.Size.X.Offset ==  0 then
		repeat
		wait()
		Humanoid.Health = Humanoid.Health -25
		until script.Parent.Size.X.Offset > 0
		end
	end
  1. Food
    In StarterPack
local Food = script.Parent
local Debounce = false
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local FoodSize = Player.PlayerGui.Statistics.Frame.Stats.Statistic.Food.FoodBar.Size
local FoodXOffset = Player.PlayerGui.Statistics.Frame.Stats.Statistic.Food.FoodBar.Size.X.Offset

Food.Equipped:Connect(function()
	Debounce = true
end)
	Food.Unequipped:Connect(function()
		Debounce = false
	end)

Mouse.Button1Down:Connect(function()
	if Debounce == false then
		return
	elseif Debounce == true then
		FoodSize = UDim2.new(0, FoodXOffset +50, 0, 13)
		print(FoodXOffset)
		script.Parent:Destroy()
			end
		end)

I know the hunger bar is considered fast, but its for testing.

Please help me resolve this.
My current error is in the second script [Statistics is not a valid member of PlayerGui].
It also persumes to print 0 in the output always, even though It should add.

1 Like

Maybe the “Statistics” Object isn’t appearing before the code runs, giving the error. You can make the code instead wait for the “Statistics” to appear on PlayerGui like this: Player.PlayerGui:WaitForChild(“Statistics”).

Also, it’s recommended to do the functional part of the hunger system on serverside, to avoid hackers. You can make a serverscript create a variable Hunger and code the system there, binding it to a PlayerEntered Event and maybe a CharacterAdded too. You then code your localscript just to update the bar to keep up with the Hunger value. To recover the hunger value with food, you create a remoteevent or function to fire from client to server, then sanity checking there.

1 Like

Ok, it goes fully through the code now, however it doesn’t seem to do the math, like +50, and the ouput always throws the number 258 (The gui’s width)

the FoodXOffset variable you made is the X offset size of the hunger bar, and that variable wont change even if you change the hunger bar size. For example, if you have a 1,1,1 Size Part on workspace, and you create a variable size = workspace.Part.Size, and then proceed to change the size of the part to 2,2,2, the variable size will still be 1,1,1. That’s probably why it prints the bar width, and probably making your hunger bar surpass the bar width by 50 when you eat.

Acually the bar stays the same depleting, doesn’t change anything.

You can actually create a textlabel to be the hunger bar and clone it inside to be the “Fill” part of the bar, using the scale properties of size instead of offset. If you want a horizontal bar use X scale, or Y scale if vertical. Then to calculate the fill bar size you make “actualhunger/maxhunger”, returning the percentage of hunger you have, which you can then set as the X or Y scale of the fill bar.

Thats because the variable FoodSize also has the same problem. You changed the variable FoodSize, not the actual size of the FoodBar.

How would I overcome this? because now it prints the depleted number the offset is currently at, just doesn’t add.

you can make:

local foodBar = Player.PlayerGui.Statistics.Frame.Stats.Statistic.Food.FoodBar
foodBar.Size = foodBar.Size + UDim2.new(0,50,0,0)

The variable actually calls the Gui Object, not a property at the time, so everytime you want to mess with the foodBar, you call it from there. To add the size of hunger bar you then sum the actual foodBar.Size with a UDim2 Value with the amount you want to add (or substract).

2 Likes

It works like a charm, thank you. UI’s on roblox are just confusing, and I always seem to be needing help with them. If only I designed game engines, I would understand why one solution doesn’t but a simular one does, again thank you for your help.

It gets easier to script as you try working on games, I learned most of Lua coding by checking poeples scripts on free models and looking at the roblox API. I just made a Hunger System if you want to look as an example, it’s here. It uses some functions/properties you might know or not, like the tweenService which is great for animations/effects, and it is server sided so people can’t mess with your game so easily, while also making sure that things replicate to the server.

1 Like

Well, I do know how to script, I have just always found GUI’s my nemisis. But yah sure I’ll check it out.

1 Like

mmm I think I already found the problem

Thats not the problem, lol, I already solved it. Also Mouse is to click to get the food. The problem was with the GUI Offset. Your a bit late to the party. Not to mention, this was in a “local script”