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)