Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I fix this error? Workspace.Food.Touch:6: attempt to index local 'hunger' (a nil value)

Asked by 5 years ago

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)
0
it means there's no "Hunger" in the playergui CjayPlyz 643 — 5y
0
make sure to check if the "H" in "Hunger" is capitilized CjayPlyz 643 — 5y
0
Basically, the output says attempt to local 'hunger' (a nil value) basically means theres no 'hunger' in the playergui CjayPlyz 643 — 5y
0
There is a hunger in the PlayerGui and it is capitalised. narrowricky -14 — 5y

1 answer

Log in to vote
0
Answered by
Lugical 425 Moderation Voter
5 years ago

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!

0
I've done what you said by putting the onTouch script in the food part which will then change this variable inside of the player's backpack called "Eating" to true and then the local script inside of where you said which has a while loop checking if the eating variable is equal to true and if so add 1 to food but it is not working narrowricky -14 — 5y
0
I found out that it is the while loop not working. narrowricky -14 — 5y
0
while script.Parent.Parent.Backpack.Eating.Value == true do narrowricky -14 — 5y
0
Nevermind it doesn't matter I have fixed it. narrowricky -14 — 5y
View all comments (2 more)
0
The server can access the PlayerGui, not just the contents of it, unless the server added the contents and they weren't there previously 1TheNoobestNoob 717 — 5y
0
^ I believe that the PlayerGui isn't accessible from the server compared to a Part, as it's part of the client technically, but yeah, it can't access content in it besides a server replicating them (which isn't the best practice) Lugical 425 — 5y
Ad

Answer this question