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)
Basically, the main reason is you are trying to access the PlayerGui from the server, which isn’t possible. Instead, you should place it in a localscript, best in starter player scripts. That way the function you are trying to work upon will still continue normally while you can access the GUI.
Also, from reading the rest, I’d add an if statement in the while loop to stop them from eating too much.
Hope this helps!