I am trying to make food eating area in my game and I made sure that the player will only eat while hunger level is below 100.
This is the error I am getting:
Workspace.Food.Touch:6: attempt to index local 'hunger' (a nil value)
This is the script that is inside of the food block:
function onTouch(hit)
local plr = hit.Parent.Name
local char = game.Players:FindFirstChild(plr)
char.Backpack.Eating.Value = true
local hunger = char.PlayerGui:FindFirstChild("Hunger")
local hungerlevel = hunger:FindFirstChild("HungerLevel")
if hungerlevel.Value < 100 then
while char.Backpack.Eating.Value == true do
char.Backpack.Food.Value = char.Backpack.Food.Value + 1
wait(1)
end
end
end
script.Parent.Touched:Connect(onTouch)

4 comments
it means there's no "Hunger" in the playergui
make sure to check if the "H" in "Hunger" is capitilized
Basically, the output says attempt to local 'hunger' (a nil value) basically means theres no 'hunger' in the playergui
There is a hunger in the PlayerGui and it is capitalised.