Issue with hunger system

I have a problem with my “hunger” system in my Roblox game. Basically I’ve been trying to fix it for a day now, and here is why:

First, there is a LocalScript in my hunger Gui. There is a tool in ServerStorage that should refill the hunger bar. I have a ProximityPrompt with a script that puts that tool in your backpack. The problem is, I have no idea what I’m doing and the tool (instead of refilling my hunger bar) does nothing, lmao

I’ve tried solutions such as moving the location of the tool and getting rid of the tool animations, but I just can’t find a solution

Hunger LocalScript:

local CS = game:GetService("CollectionService")

local Hunger = script.Parent:WaitForChild("Hunger")
local Bar = script.Parent:WaitForChild("Bar")

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")


local function Update()
	Bar.Size = UDim2.new(Hunger.Value / 100, 0, 1, 0)
end


coroutine.wrap(function()
	while task.wait(2) do
		if Hunger.Value > 0 then
			Hunger.Value -= 1
			
			if Hunger.Value == 0 then
				hum.Health = 0
			end
		end
	end
end)()


for tools, tool in pairs(CS:GetTagged("Food")) do
	tool.Activated:Connect(function()
		local newVal = math.clamp(Hunger.Value + tool.Gain.Value, 0, 100)
		
		tool.Enabled = false
		tool:Destroy()
		
		Hunger.Value = newVal
	end)
end


Hunger:GetPropertyChangedSignal("Value"):Connect(Update)

The Debounce script, (thanks to a fellow @ImTheBestMayne)

local ProximityPrompt = script.Parent
local debounceTime = 5
local isDebouncing = false
local ToolName = "Almond Water"
local Storage = game.ServerStorage

ProximityPrompt.Triggered:Connect(function(player)
	if not isDebouncing then
		isDebouncing = true
		if player and player.Character then
			local Backpack = player:FindFirstChild("Backpack")
			if not Backpack then
				Backpack = Instance.new("Folder")
				Backpack.Name = "Backpack"
				Backpack.Parent = player
			end

			local Tool = Storage:FindFirstChild(ToolName)
			if Tool then
				Tool:Clone().Parent = Backpack
				print("proximityPrompt triggered")
				wait(debounceTime)
				isDebouncing = false
			else
				print("almond water not found")
				wait(debounceTime)
				isDebouncing = false
			end
		else
			print("player not found")
			isDebouncing = false
		end
	elseif isDebouncing then
		print("You cannot use this right now.")
	end
end)

(The tool does have a tag called “Food,” and also has the required Gain value)

3 Likes

To me, it looks like this-

for tools, tool in pairs(CS:GetTagged("Food")) do
	tool.Activated:Connect(function()
		local newVal = math.clamp(Hunger.Value + tool.Gain.Value, 0, 100)
		
		tool.Enabled = false
		tool:Destroy()
		
		Hunger.Value = newVal
	end)
end

-section of code would run before the tool exists in the players backpack.

(meaning it will do nothing, as CS:GetTagged() would not have included a not-yet existing instance and will not bind the event listeners to your new tool)

Instead, maybe listen for a .ChildAdded event on the player’s backpack to then listen for tool.Activated?

1 Like

Sorry for the late response, but I changed the script up and the vending machine (instead of giving me the broken item) just refills my hunger bar when I interact with the ProximityPrompt.

local CS = game:GetService("CollectionService")

local Hunger = script.Parent:WaitForChild("Hunger")
local Bar = script.Parent:WaitForChild("Bar")

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")


local function Update()
	Bar.Size = UDim2.new(Hunger.Value / 100, 0, 1, 0)
end


coroutine.wrap(function()
	while task.wait(2) do
		if Hunger.Value > 0 then
			Hunger.Value -= 1
			
			if Hunger.Value == 0 then
				hum.Health = 0
			end
		end
	end
end)()


local backpack = plr:WaitForChild("Backpack")

backpack.ChildAdded:Connect(function(item)
	if CS:HasTag(item, "Food") then
		local newVal = math.clamp(Hunger.Value + item.Gain.Value, 0, 100)

		item:Destroy()

		Hunger.Value = newVal
	end
end)


Hunger:GetPropertyChangedSignal("Value"):Connect(Update)