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

Workspace.Land.Model.Detector.Script:6: attempt to index local 'Thirst' (a nil value)?

Asked by 5 years ago

any explanation?

local clickdetect = script.Parent:WaitForChild("ClickDetector")

clickdetect.MouseClick:Connect(function(plr)
    print(plr)
    local Thirst = plr:FindFirstChild("ThirstVal")
    if Thirst.Value >100 then
        Thirst.Value = 100
    end
    Thirst.Value = 100
end)
0
it means that ThirstVal is not a child of plr HappyTimIsHim 652 — 5y
0
if ThirstVal isn't a child of plr then it returns nil meaning ur doing nil.Value HappyTimIsHim 652 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Problem

Workspace.Land.Model.Detector.Script:6: attempt to index local 'Thirst' (a nil value)?

Whats wrong

This means that Thirst is nil. You can't index something that is nil since that makes no sense.

Example code

local a = nil

print(a.b)

This will output an error attempt to index local 'a' (a nil value) since a is nil and not a table or a userdata or has no metatable with a __index set.

Solution

Think about what your code is doing. You're looking for something called ThirstVal specifically inside plr. Perhaps you have it in a specific folder, or its inside their Character. To index a player's character use the .Character property. (plr.Character)

To prevent this error for now you can check if Thirst even exists in the first place using an if statement.

local clickDetect = ...

local function clicked(player)
    print(player)

    local thirst = player:FindFirstChild("ThirstVal")

    if thirst then --// make sure it actually exists
        --// logic checks for 
    else --// warn it doesn't exist and print the reference point to your player. 
        warn("no thirst value inside of: " ..  player:GetFullName())
    end
end

clickDetect.MouseClick:Connect(clicked)

Though again, I do suggest making sure you check where the ThirstVal is actually being added. Perhaps try indexing the player's character and looking for it.

Ad

Answer this question