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

how to fix attempt to index nil with 'Value'?

game.Players.PlayerAdded:Connect(function(Player)
	
	Player.Backpack:FindFirstChild("Oddity").Value = game.ServerStorage.Odditys.Oddity1
	
	if Player.Backpack:FindFirstChild("Oddity").Value == game.ServerStorage.Odditys.Oddity1 or game.ServerStorage.Odditys.Oddity2 then
		
		local Oddity = Player.Backpack.Oddity.Value
		Oddity:Clone().Parent = Player.Backpack
		
	end
	
end)

ERROR = ServerScriptService.DataService:3: attempt to index nil with 'Value' line = 3 the value is an object value

1 comment

0

It means the object doesnt exist in serverstorage. Like the value you are trying to copy from

1 answer

✓ Accepted answer

This is because you are using .Value but the :FindFirstChild("Oddity") may not find Oddity and therefore it returns nil. It's the same as saying nil.Value hence the error.

To fix this you should try :FindFirstChild() and on the next line check if it wasn't nil

game.Players.PlayerAdded:Connect(function(Player)
	local oddity = Player.Backpack:FindFirstChild("Oddity")
	if oddity then -- check if it's not nil then move on
		oddity.Value = game.ServerStorage.Odditys.Oddity1

		if oddity.Value == game.ServerStorage.Odditys.Oddity1 then
			-- whatever
		end
	else
		warn("Could not find Oddity in player backpack")
	end
end)

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