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

"attempt to index nil with 'Value'" error. How do I fix this?

The script's intended purpose is to detect when its parent object is touched, then add 1 to an IntValue contained in StarterCharacterScripts. I get the aforementioned error on line 13. Shouldn't I be able to access the "Value" property like I can access "Color" and "Transparency" on things such as parts? And yes, I know there is a bit of spaghetti going on here.

stimPack = script.Parent

local function onPartTouch(otherPart)
		
	local partParent = otherPart.Parent	
	local humanoid = partParent:FindFirstChildWhichIsA("Humanoid")
	
	if humanoid then
		
		local character = humanoid.Parent
		local player = game.Players:GetPlayerFromCharacter(character)
		local stims = player:FindFirstChild("Stims")
		local stimvalue = stims.Value
		
		print(player, "has picked up a Syringe!")
		
		stimValue += 1
		
	end
	
end

stimPack.Touched:Connect(onPartTouch)

3 comments

0

i dont think its the value that has the problem but the stims var

0

this is how i see it: your stims value is in the character but your stims var searches the Player and therefore the game couldnt find stims because stims is never there in the player because its in the character

0

if the game couldnt find something that exists it is automatically set to nil and the game gets "nil.Value"

1 answer

✓ Accepted answer

line 12

 local stims = player:FindFirstChild("Stims")

why are you searching inside the player when Stimsvalue is in the character

it would make more sense to do

local stims = character:FindFirstChild("Stims")

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