Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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)

2 comments

0

it means that ThirstVal is not a child of plr

0

if ThirstVal isn't a child of plr then it returns nil meaning ur doing nil.Value

1 answer

##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.

This discussion is preserved as it was. New answers, comments, and votes are closed.